/*
 * 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/Core/DocumentService","TYPO3/CMS/Core/Event/RegularEvent","./Viewport"],(function(e,t,n,a,i){"use strict";return new class{constructor(){this.options={onChangeSelector:'[data-global-event="change"]',onClickSelector:'[data-global-event="click"]',onSubmitSelector:'form[data-global-event="submit"]'},n.ready().then(()=>this.registerEvents())}registerEvents(){new a("change",this.handleChangeEvent.bind(this)).delegateTo(document,this.options.onChangeSelector),new a("click",this.handleClickEvent.bind(this)).delegateTo(document,this.options.onClickSelector),new a("submit",this.handleSubmitEvent.bind(this)).delegateTo(document,this.options.onSubmitSelector)}handleChangeEvent(e,t){e.preventDefault(),this.handleFormChildAction(e,t)||this.handleFormChildNavigateAction(e,t)}handleClickEvent(e,t){e.preventDefault(),this.handleFormChildAction(e,t)}handleSubmitEvent(e,t){e.preventDefault(),this.handleFormNavigateAction(e,t)}handleFormChildAction(e,t){const n=t.dataset.actionSubmit,a=t.dataset.actionFocus;if(!n&&!a)return!1;const i=t.closest("form");if(n){if("$form"===n&&this.isHTMLFormChildElement(t))return t.form.submit(),!0;const e=document.querySelector(n);return e instanceof HTMLFormElement&&(e.submit(),!0)}if(a&&i){if(!(i instanceof HTMLFormElement))return!1;const e=i.querySelector(a);if(null===e)return!1;e.focus()}return!0}handleFormChildNavigateAction(e,t){const n=t.dataset.actionNavigate;if(!n)return!1;const a=this.resolveHTMLFormChildElementValue(t),l=t.dataset.navigateValue;let o=null;return"$data=~s/$value/"===n&&l&&null!==a?o=this.substituteValueVariable(l,a):"$data"===n&&l?o=l:"$value"===n&&a&&(o=a),null!==o&&(window===i.ContentContainer.get()?i.ContentContainer.setUrl(o):window.location.href=o,!0)}handleFormNavigateAction(e,t){const n=t.action,a=t.dataset.actionNavigate;if(!n||!a)return!1;const l=t.dataset.navigateValue,o=t.dataset.valueSelector,r=this.resolveHTMLFormChildElementValue(t.querySelector(o));let s=null;return"$form=~s/$value/"===a&&l&&null!==r?s=this.substituteValueVariable(l,r):"$form"===a&&(s=n),null!==s&&(window===i.ContentContainer.get()?i.ContentContainer.setUrl(s):window.location.href=s,!0)}substituteValueVariable(e,t){return e.replace(/(\$\{value\}|%24%7Bvalue%7D|\$\[value\]|%24%5Bvalue%5D)/gi,t)}isHTMLFormChildElement(e){return e instanceof HTMLSelectElement||e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}resolveHTMLFormChildElementValue(e){const t=e.getAttribute("type");if(e instanceof HTMLSelectElement)return e.options[e.selectedIndex].value;if(e instanceof HTMLInputElement&&"checkbox"===t){const t=e.dataset.emptyValue;return e.checked?e.value:void 0!==t?t:""}return e instanceof HTMLInputElement?e.value:null}}}));