/*
 * 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!
 */
import Y from"@typo3/core/document-service.js";import l from"jquery";import{SeverityEnum as L}from"@typo3/backend/enum/severity.js";import O from"@typo3/core/ajax/ajax-request.js";import p from"@typo3/backend/icons.js";import P from"@typo3/backend/modal.js";import e from"@typo3/backend/multi-step-wizard.js";import"@typo3/backend/element/icon-element.js";class _{constructor(){this.triggerButton=".t3js-localize",Y.ready().then(()=>{this.initialize()})}async initialize(){const m=await p.getIcon("actions-localize",p.sizes.large),h=await p.getIcon("actions-edit-copy",p.sizes.large);l(this.triggerButton).removeClass("disabled"),l(document).on("click",this.triggerButton,async v=>{v.preventDefault();const a=l(v.currentTarget),y=[],w=[];if(a.data("allowTranslate")===0&&a.data("allowCopy")===0){P.confirm(TYPO3.lang["window.localization.mixed_mode.title"],TYPO3.lang["window.localization.mixed_mode.message"],L.warning,[{text:TYPO3?.lang?.["button.ok"]||"OK",btnClass:"btn-warning",name:"ok",trigger:(n,t)=>t.hideModal()}]);return}const I=await(await this.loadAvailableLanguages(parseInt(a.data("pageId"),10),parseInt(a.data("languageId"),10))).resolve();if(a.data("allowTranslate")&&(y.push('<div class="row"><div class="col-sm-3"><input class="btn-check t3js-localization-option" type="radio" name="mode" id="mode_translate" value="localize"><label class="btn btn-default btn-block-vertical" for="mode_translate" data-action="localize">'+m+TYPO3.lang["localize.wizard.button.translate"]+'</label></div><div class="col-sm-9"><p class="text-body-secondary">'+TYPO3.lang["localize.educate.translate"]+"</p></div></div>"),w.push("localize")),a.data("allowCopy")&&(y.push('<div class="row"><div class="col-sm-3"><input class="btn-check t3js-localization-option" type="radio" name="mode" id="mode_copy" value="copyFromLanguage"><label class="btn btn-default btn-block-vertical" for="mode_copy" data-action="copy">'+h+TYPO3.lang["localize.wizard.button.copy"]+'</label></div><div class="col-sm-9"><p class="t3js-helptext t3js-helptext-copy text-body-secondary">'+TYPO3.lang["localize.educate.copy"]+"</p></div></div>"),w.push("copyFromLanguage")),w.length===1)e.set("localizationMode",w[0]);else{const n=document.createElement("div");n.dataset.bsToggle="buttons",n.append(...y.map(t=>document.createRange().createContextualFragment(t))),e.addSlide("localize-choose-action",TYPO3.lang["localize.wizard.header_page"].replace("{0}",a.data("page")).replace("{1}",a.data("languageName")),n,L.notice,TYPO3.lang["localize.wizard.step.selectMode"],(t,s)=>{s.localizationMode!==void 0&&e.unlockNextStep()})}I.length===1?e.set("sourceLanguage",I[0].uid):e.addSlide("localize-choose-language",TYPO3.lang["localize.view.chooseLanguage"],"",L.notice,TYPO3.lang["localize.wizard.step.chooseLanguage"],async(n,t)=>{t.sourceLanguage!==void 0&&e.unlockNextStep(),n.html('<div class="text-center">'+await p.getIcon("spinner-circle",p.sizes.large)+"</div>"),e.getComponent().on("change",".t3js-language-option",r=>{e.set("sourceLanguage",l(r.currentTarget).val()),e.unlockNextStep()});const s=l("<div />",{class:"row"});for(const r of I){const C="language"+r.uid,o=l("<input />",{type:"radio",name:"language",id:C,value:r.uid,class:"btn-check t3js-language-option"}),c=l("<label />",{class:"btn btn-default btn-block",for:C}).text(" "+r.title).prepend(r.flagIcon);s.append(l("<div />",{class:"col-sm-4"}).append(o).append(c))}n.empty().append(s)}),e.addSlide("localize-summary",TYPO3.lang["localize.view.summary"],"",L.notice,TYPO3.lang["localize.wizard.step.selectRecords"],async(n,t)=>{n.empty().html('<div class="text-center">'+await p.getIcon("spinner-circle",p.sizes.large)+"</div>");const s=await(await this.getSummary(parseInt(a.data("pageId"),10),parseInt(a.data("languageId"),10),t.sourceLanguage)).resolve();n.empty(),e.set("records",[]);const r=s.columns.columns;s.columns.columnList.forEach(o=>{if(typeof s.records[o]>"u")return;const c=r[o],d=document.createElement("div");d.classList.add("row","gy-2"),s.records[o].forEach(i=>{const S=" ("+i.uid+") "+i.title;t.records.push(i.uid);const T=document.createElement("div");T.classList.add("col-sm-6");const x=document.createElement("div");x.classList.add("input-group");const E=document.createElement("span");E.classList.add("input-group-text");const j=document.createElement("span");j.classList.add("form-check","form-check-type-toggle");const b=document.createElement("input");b.type="checkbox",b.id="record-uid-"+i.uid,b.classList.add("form-check-input","t3js-localization-toggle-record"),b.checked=!0,b.dataset.uid=i.uid.toString(),b.ariaLabel=S;const k=document.createElement("label");k.classList.add("form-control"),k.htmlFor="record-uid-"+i.uid,k.innerHTML=i.icon,k.appendChild(document.createTextNode(S)),j.appendChild(b),E.appendChild(j),x.appendChild(E),x.appendChild(k),T.appendChild(x),d.appendChild(T)});const u=document.createElement("fieldset");u.classList.add("localization-fieldset");const f=document.createElement("div");f.classList.add("form-check","form-check-type-toggle");const z=document.createElement("input");z.classList.add("form-check-input","t3js-localization-toggle-column"),z.id="records-column-"+o,z.type="checkbox",z.checked=!0;const g=document.createElement("label");g.classList.add("form-check-label"),g.htmlFor="records-column-"+o,g.textContent=c,f.appendChild(z),f.appendChild(g),u.appendChild(f),u.appendChild(d),n.append(u)}),e.unlockNextStep(),e.getComponent().on("change",".t3js-localization-toggle-record",o=>{const c=l(o.currentTarget),d=c.data("uid"),u=c.closest("fieldset"),f=u.find(".t3js-localization-toggle-column");if(c.is(":checked"))t.records.push(d);else{const i=t.records.indexOf(d);i>-1&&t.records.splice(i,1)}const z=u.find(".t3js-localization-toggle-record"),g=u.find(".t3js-localization-toggle-record:checked");f.prop("checked",g.length>0),f.prop("__indeterminate",g.length>0&&g.length<z.length),t.records.length>0?e.unlockNextStep():e.lockNextStep()}).on("change",".t3js-localization-toggle-column",o=>{const c=l(o.currentTarget),d=c.closest("fieldset").find(".t3js-localization-toggle-record");d.prop("checked",c.is(":checked")),d.trigger("change")})}),e.addFinalProcessingSlide(async(n,t)=>{await this.localizeRecords(parseInt(a.data("pageId"),10),parseInt(a.data("languageId"),10),t.sourceLanguage,t.localizationMode,t.records),e.dismiss(),document.location.reload()}).then(()=>{e.show(),e.getComponent().on("change",".t3js-localization-option",n=>{e.set("localizationMode",l(n.currentTarget).val()),e.unlockNextStep()})})})}loadAvailableLanguages(m,h){return new O(TYPO3.settings.ajaxUrls.page_languages).withQueryArguments({pageId:m,languageId:h}).get()}getSummary(m,h,v){return new O(TYPO3.settings.ajaxUrls.records_localize_summary).withQueryArguments({pageId:m,destLanguageId:h,languageId:v}).get()}localizeRecords(m,h,v,a,y){return new O(TYPO3.settings.ajaxUrls.records_localize).withQueryArguments({pageId:m,srcLanguageId:v,destLanguageId:h,action:a,uidList:y}).get()}}var M=new _;export{M as default};
