/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};define(["require","exports","jquery","../AjaxDataHandler","../Icons","jquery-ui/droppable"],(function(e,t,a,o,s){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),a=__importDefault(a);class r{static initialize(){(0,a.default)(r.contentIdentifier).draggable({handle:r.dragHeaderIdentifier,scope:"tt_content",cursor:"move",distance:20,revert:"invalid",zIndex:100,start:e=>{r.onDragStart((0,a.default)(e.target))},stop:e=>{r.onDragStop((0,a.default)(e.target))}}),(0,a.default)(r.dropZoneIdentifier).droppable({accept:this.contentIdentifier,scope:"tt_content",tolerance:"pointer",over:(e,t)=>{r.onDropHoverOver((0,a.default)(t.draggable),(0,a.default)(e.target))},out:(e,t)=>{r.onDropHoverOut((0,a.default)(t.draggable),(0,a.default)(e.target))},drop:(e,t)=>{r.onDrop((0,a.default)(t.draggable),(0,a.default)(e.target),e)}})}static onDragStart(e){r.originalStyles=e.get(0).style.cssText,e.children(r.dragIdentifier).addClass("dragitem-shadow"),e.append('<div class="ui-draggable-copy-message">'+TYPO3.lang["dragdrop.copy.message"]+"</div>"),e.children(r.dropZoneIdentifier).addClass("drag-start"),e.closest(r.columnIdentifier).removeClass("active"),e.find(r.dropZoneIdentifier).hide(),(0,a.default)(r.dropZoneIdentifier).each((e,t)=>{const o=(0,a.default)(t);o.parent().find(r.addContentIdentifier).length&&o.addClass(r.validDropZoneClass)})}static onDragStop(e){e.children(r.dragIdentifier).removeClass("dragitem-shadow"),e.children(r.dropZoneIdentifier).removeClass("drag-start"),e.closest(r.columnIdentifier).addClass("active"),e.find(r.dropZoneIdentifier).show(),e.find(".ui-draggable-copy-message").remove(),e.get(0).style.cssText=r.originalStyles,(0,a.default)(r.dropZoneIdentifier+"."+r.validDropZoneClass).removeClass(r.validDropZoneClass)}static onDropHoverOver(e,t){t.hasClass(r.validDropZoneClass)&&t.addClass(r.dropPossibleHoverClass)}static onDropHoverOut(e,t){t.removeClass(r.dropPossibleHoverClass)}static onDrop(e,t,o){const n=r.getColumnPositionForElement(t);t.removeClass(r.dropPossibleHoverClass);const d=parseInt(e.data("uid"),10);if("number"==typeof d&&d>0){let i={};const l=t.closest(r.contentIdentifier).data("uid");let c=0;c=void 0===l?parseInt(o.target.offsetParent.getAttribute("data-page"),10):0-parseInt(l,10);let p=parseInt(e.data("language-uid"),10);-1!==p&&(p=parseInt(t.closest("[data-language-uid]").data("language-uid"),10));let g=0;0!==c&&(g=n);const f=o&&o.originalEvent.ctrlKey||t.hasClass("t3js-paste-copy"),u=f?"copy":"move";i.cmd={tt_content:{[d]:{[u]:{action:"paste",target:c,update:{colPos:g,sys_language_uid:p}}}}},r.ajaxAction(t,e,i,f).then(()=>{const t=(0,a.default)(`.t3-page-column-lang-name[data-language-uid="${p}"]`);if(0===t.length)return;const o=t.data("flagIdentifier"),r=t.data("languageTitle");s.getIcon(o,s.sizes.small).then(t=>{e.find(".t3js-flag").attr("title",r).html(t)})})}}static ajaxAction(e,t,a,s){const n=Object.keys(a.cmd).shift(),d=parseInt(Object.keys(a.cmd[n]).shift(),10),i={component:"dragdrop",action:s?"copy":"move",table:n,uid:d};return o.process(a,i).then(a=>{if(a.hasErrors)throw a.messages;e.parent().hasClass(r.contentIdentifier.substring(1))?t.detach().css({top:0,left:0}).insertAfter(e.closest(r.contentIdentifier)):t.detach().css({top:0,left:0}).insertAfter(e.closest(r.dropZoneIdentifier)),s&&self.location.reload()})}static getColumnPositionForElement(e){const t=e.closest("[data-colpos]");return!(!t.length||"undefined"===t.data("colpos"))&&t.data("colpos")}}r.contentIdentifier=".t3js-page-ce",r.dragIdentifier=".t3-page-ce-dragitem",r.dragHeaderIdentifier=".t3js-page-ce-draghandle",r.dropZoneIdentifier=".t3js-page-ce-dropzone-available",r.columnIdentifier=".t3js-page-column",r.validDropZoneClass="active",r.dropPossibleHoverClass="t3-page-ce-dropzone-possible",r.addContentIdentifier=".t3js-page-new-ce",r.originalStyles="",t.default=r,(0,a.default)(r.initialize)}));