/*
 * 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 u from"@typo3/backend/hotkeys/hotkey-storage.js";import f from"@typo3/core/event/regular-event.js";var s;(function(c){c.META="meta",c.CTRL="control",c.SHIFT="shift",c.ALT="alt"})(s||(s={}));class y{constructor(){this.normalizedCtrlModifierKey=navigator.platform.toLowerCase().startsWith("mac")?s.META:s.CTRL,this.defaultOptions={scope:"all",allowOnEditables:!1,allowRepeat:!1,bindElement:void 0},this.scopedHotkeyMap=u.getScopedHotkeyMap(),this.setScope("all"),this.registerEventHandler()}setScope(t){u.activeScope=t}getScope(){return u.activeScope}register(t,e,o={}){if(t.filter(a=>!Object.values(s).includes(a)).length===0)throw new Error('Attempted to register hotkey "'+t.join("+")+'" without a non-modifier key.');t=t.map(a=>a.toLowerCase());const r={...this.defaultOptions,...o};this.scopedHotkeyMap.has(r.scope)||this.scopedHotkeyMap.set(r.scope,new Map);let i=this.composeAriaKeyShortcut(t);const n=this.scopedHotkeyMap.get(r.scope),p=this.createHotkeyStructFromTrigger(t),l=JSON.stringify(p);if(n.has(l)&&(n.get(l).options.bindElement?.removeAttribute("aria-keyshortcuts"),n.delete(l)),n.set(l,{struct:p,handler:e,options:r}),r.bindElement instanceof Element){const a=r.bindElement.getAttribute("aria-keyshortcuts");a!==null&&!a.includes(i)&&(i=a+" "+i),r.bindElement.setAttribute("aria-keyshortcuts",i)}}registerEventHandler(){new f("keydown",t=>{const e=this.findHotkeySetup(t);if(e!==null&&!(t.repeat&&!e.options.allowRepeat)){if(!e.options.allowOnEditables){const o=t.target;if(o.isContentEditable||["INPUT","TEXTAREA","SELECT"].includes(o.tagName)&&!t.target.readOnly)return}e.handler(t)}}).bindTo(document)}findHotkeySetup(t){const e=[...new Set(["all",u.activeScope])],o=this.createHotkeyStructFromEvent(t),r=JSON.stringify(o);for(const i of e){const n=this.scopedHotkeyMap.get(i);if(n.has(r))return n.get(r)}return null}createHotkeyStructFromTrigger(t){const e=t.filter(o=>!Object.values(s).includes(o));if(e.length>1)throw new Error('Cannot register hotkey with more than one non-modifier key, "'+e.join("+")+'" given.');return{modifiers:{meta:t.includes(s.META),ctrl:t.includes(s.CTRL),shift:t.includes(s.SHIFT),alt:t.includes(s.ALT)},key:e[0].toLowerCase()}}createHotkeyStructFromEvent(t){return{modifiers:{meta:t.metaKey,ctrl:t.ctrlKey,shift:t.shiftKey,alt:t.altKey},key:t.key?.toLowerCase()}}composeAriaKeyShortcut(t){const e=[];for(let o of t)o==="+"?o="plus":o=o.replace(/[\u00A0-\u9999<>&]/g,r=>"&#"+r.charCodeAt(0)+";"),e.push(o);return e.sort((o,r)=>{const i=Object.values(s).includes(o),n=Object.values(s).includes(r);return i&&!n?-1:!i&&n?1:i&&n?-1:0}),e.join("+")}}let d;TYPO3.Hotkeys?d=TYPO3.Hotkeys:(d=new y,TYPO3.Hotkeys=d);var h=d;export{s as ModifierKeys,h as default};
