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