/*
 * 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!
 */
define(["require","exports","TYPO3/CMS/Backend/Notification","TYPO3/CMS/Core/DocumentService","TYPO3/CMS/Core/Event/RegularEvent"],(function(e,t,c,o,n){"use strict";var i,l,a,s;!function(e){e.actionsSelector=".t3js-multi-record-selection-actions",e.checkboxSelector=".t3js-multi-record-selection-check",e.checkboxActionsSelector=".t3js-multi-record-selection-check-actions",e.checkboxActionsToggleSelector=".t3js-multi-record-selection-check-actions-toggle",e.rowSelectionSelector="[data-multi-record-selection-row-selection] tr"}(i||(i={})),function(e){e.actionButton="button[data-multi-record-selection-action]",e.checkboxActionButton="button[data-multi-record-selection-check-action]"}(l||(l={})),function(e){e.checkAll="check-all",e.checkNone="check-none",e.toggle="toggle"}(a||(a={})),function(e){e.any="",e.checked=":checked",e.unchecked=":not(:checked)"}(s||(s={}));class r{constructor(){this.lastChecked=null,o.ready().then(()=>{r.restoreTemporaryState(),this.registerActions(),this.registerActionsEventHandlers(),this.registerCheckboxActions(),this.registerCheckboxKeyboardActions(),this.registerCheckboxTableRowSelectionAction(),this.registerToggleCheckboxActions(),this.registerDispatchCheckboxStateChangedEvent(),this.registerCheckboxStateChangedEventHandler()})}static getCheckboxes(e=s.any,t=""){return document.querySelectorAll(r.getCombinedSelector(i.checkboxSelector+e,t))}static getCombinedSelector(e,t){return""!==t?['[data-multi-record-selection-identifier="'+t+'"]',e].join(" "):e}static getIdentifier(e){var t;return(null===(t=e.closest("[data-multi-record-selection-identifier]"))||void 0===t?void 0:t.dataset.multiRecordSelectionIdentifier)||""}static changeCheckboxState(e,t){e.checked===t||e.dataset.manuallyChanged||(e.checked=t,e.dispatchEvent(new CustomEvent("multiRecordSelection:checkbox:state:changed",{detail:{identifier:r.getIdentifier(e)},bubbles:!0,cancelable:!1})))}static restoreTemporaryState(){const e=r.getCheckboxes(s.checked);if(!e.length)return;let t=!1,c=[];e.forEach(e=>{e.closest("tr").classList.add("success");const o=r.getIdentifier(e);""===o||c.includes(o)||(c.push(o),t=!0,r.toggleActionsState(o))}),t||r.toggleActionsState()}static toggleActionsState(e=""){const t=document.querySelectorAll(r.getCombinedSelector(i.actionsSelector,e));if(!t.length)return;if(!r.getCheckboxes(s.checked,e).length)return void t.forEach(e=>r.changeActionContainerVisibility(e,!1));t.forEach(e=>r.changeActionContainerVisibility(e));const c=document.querySelectorAll([r.getCombinedSelector(i.actionsSelector,e),l.actionButton].join(" "));c.length&&c.forEach(t=>{if(!t.dataset.multiRecordSelectionActionConfig)return;const c=JSON.parse(t.dataset.multiRecordSelectionActionConfig);if(!c.idField)return;t.classList.add("disabled");const o=r.getCheckboxes(s.checked,e);for(let e=0;e<o.length;e++)if(o[e].closest("tr").dataset[c.idField]){t.classList.remove("disabled");break}})}static changeActionContainerVisibility(e,t=!0){var c;const o=null===(c=e.closest(".multi-record-selection-panel"))||void 0===c?void 0:c.children;if(t){if(o)for(let e=0;e<o.length;e++)o[e].classList.add("hidden");e.classList.remove("hidden")}else{if(o)for(let e=0;e<o.length;e++)o[e].classList.remove("hidden");e.classList.add("hidden")}}static unsetManuallyChangedAttribute(e){r.getCheckboxes(s.any,e).forEach(e=>{e.removeAttribute("data-manually-changed")})}registerActions(){new n("click",(e,t)=>{t.dataset.multiRecordSelectionAction;const c=r.getIdentifier(t),o=JSON.parse(t.dataset.multiRecordSelectionActionConfig||"{}"),n=r.getCheckboxes(s.checked,c);n.length&&t.dispatchEvent(new CustomEvent("multiRecordSelection:action:"+t.dataset.multiRecordSelectionAction,{detail:{identifier:c,checkboxes:n,configuration:o},bubbles:!0,cancelable:!1}))}).delegateTo(document,[i.actionsSelector,l.actionButton].join(" "))}registerActionsEventHandlers(){new n("multiRecordSelection:actions:show",e=>{var t;const c=(null===(t=e.detail)||void 0===t?void 0:t.identifier)||"",o=document.querySelectorAll(r.getCombinedSelector(i.actionsSelector,c));o.length&&o.forEach(e=>r.changeActionContainerVisibility(e))}).bindTo(document),new n("multiRecordSelection:actions:hide",e=>{var t;const c=(null===(t=e.detail)||void 0===t?void 0:t.identifier)||"",o=document.querySelectorAll(r.getCombinedSelector(i.actionsSelector,c));o.length&&o.forEach(e=>r.changeActionContainerVisibility(e,!1))}).bindTo(document)}registerCheckboxActions(){new n("click",(e,t)=>{if(e.preventDefault(),!t.dataset.multiRecordSelectionCheckAction)return;const o=r.getIdentifier(t),n=r.getCheckboxes(s.any,o);if(n.length){switch(r.unsetManuallyChangedAttribute(o),t.dataset.multiRecordSelectionCheckAction){case a.checkAll:n.forEach(e=>{r.changeCheckboxState(e,!0)});break;case a.checkNone:n.forEach(e=>{r.changeCheckboxState(e,!1)});break;case a.toggle:n.forEach(e=>{r.changeCheckboxState(e,!e.checked)});break;default:c.warning("Unknown checkbox action")}r.unsetManuallyChangedAttribute(o)}}).delegateTo(document,[i.checkboxActionsSelector,l.checkboxActionButton].join(" "))}registerCheckboxKeyboardActions(){new n("click",(e,t)=>this.handleCheckboxKeyboardActions(e,t)).delegateTo(document,i.checkboxSelector)}registerCheckboxTableRowSelectionAction(){new n("click",(e,t)=>{const c=e.target.tagName;if("TH"!==c&&"TD"!==c)return;const o=t.querySelector(i.checkboxSelector);null!==o&&(r.changeCheckboxState(o,!o.checked),this.handleCheckboxKeyboardActions(e,o,!1))}).delegateTo(document,i.rowSelectionSelector),new n("mousedown",e=>(e.shiftKey||e.altKey||e.ctrlKey)&&e.preventDefault()).delegateTo(document,i.rowSelectionSelector)}registerDispatchCheckboxStateChangedEvent(){new n("change",(e,t)=>{t.dispatchEvent(new CustomEvent("multiRecordSelection:checkbox:state:changed",{detail:{identifier:r.getIdentifier(t)},bubbles:!0,cancelable:!1}))}).delegateTo(document,i.checkboxSelector)}registerCheckboxStateChangedEventHandler(){new n("multiRecordSelection:checkbox:state:changed",e=>{var t;const c=e.target,o=(null===(t=e.detail)||void 0===t?void 0:t.identifier)||"";c.checked?c.closest("tr").classList.add("success"):c.closest("tr").classList.remove("success"),r.toggleActionsState(o)}).bindTo(document)}registerToggleCheckboxActions(){new n("click",(e,t)=>{const c=r.getIdentifier(t),o=document.querySelector([r.getCombinedSelector(i.checkboxActionsSelector,c),'button[data-multi-record-selection-check-action="'+a.checkAll+'"]'].join(" "));null!==o&&o.classList.toggle("disabled",!r.getCheckboxes(s.unchecked,c).length);const n=document.querySelector([r.getCombinedSelector(i.checkboxActionsSelector,c),'button[data-multi-record-selection-check-action="'+a.checkNone+'"]'].join(" "));null!==n&&n.classList.toggle("disabled",!r.getCheckboxes(s.checked,c).length);const l=document.querySelector([r.getCombinedSelector(i.checkboxActionsSelector,c),'button[data-multi-record-selection-check-action="'+a.toggle+'"]'].join(" "));null!==l&&l.classList.toggle("disabled",!r.getCheckboxes(s.any,c).length)}).delegateTo(document,i.checkboxActionsToggleSelector)}handleCheckboxKeyboardActions(e,t,c=!0){const o=r.getIdentifier(t);if(this.lastChecked&&document.body.contains(this.lastChecked)&&r.getIdentifier(this.lastChecked)===o&&(e.shiftKey||e.altKey||e.ctrlKey)){if(c&&r.unsetManuallyChangedAttribute(o),e.shiftKey){const e=Array.from(r.getCheckboxes(s.any,o)),c=e.indexOf(t),n=e.indexOf(this.lastChecked);e.slice(Math.min(c,n),Math.max(c,n)+1).forEach(e=>{e!==t&&r.changeCheckboxState(e,t.checked)})}this.lastChecked=t,(e.altKey||e.ctrlKey)&&r.getCheckboxes(s.any,o).forEach(e=>{e!==t&&r.changeCheckboxState(e,!e.checked)}),r.unsetManuallyChangedAttribute(o)}else this.lastChecked=t}}return new r}));