/**
 * Copyright 2023
 * @license , see License.md for full text.
 */
import{localStorageSet as t,localStorageGet as e,localStorageDelete as a,validURL as i}from"../utils/utils.js";import{observable as r,makeObservable as s,computed as o,configure as l,autorun as n,toJS as c}from"../../mobx/dist/mobx.esm.js";l({enforceActions:!1}),globalThis.UserScaffold=globalThis.UserScaffold||{},globalThis.UserScaffold.requestAvailability=()=>(!globalThis.UserScaffold.instance&&globalThis.document&&globalThis.document.body&&(globalThis.UserScaffold.instance=globalThis.document.createElement("user-scaffold"),globalThis.document.body.appendChild(globalThis.UserScaffold.instance)),globalThis.UserScaffold.instance);export const UserScaffoldInstance=globalThis.UserScaffold.requestAvailability();export class UserScaffold extends HTMLElement{static get tag(){return"user-scaffold"}constructor(){super(),this.debug=!1,this.windowControllers=new AbortController,this.stMemory={interactionDelay:0,interactionCount:0},this.ltMemory=e("user-scaffold-ltMemory",{}),this.action={type:null,architype:null},this.data={raw:null,value:null,architype:null},this.active=!0,this.userActionArchitypes(),s(this,{debug:r,stMemory:r,ltMemory:r,action:r,data:r,active:r,memory:o}),n((()=>{this.debug&&console.trace(this)}))}userActionArchitypes(){this.interactionInterval=setInterval((()=>{this.active&&this.readMemory("interactionDelay")<=3e3&&this.incrementWriteMemory("interactionDelay",600)}),300),globalThis.addEventListener("click",this.userMouseAction.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("drop",this.userDropAction.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("dragover",this.userDragAction.bind(this),{signal:this.windowControllers.signal})}userKeyDownAction(t){t.isTrusted&&(this.action={type:"key",architype:"input"},this.data={raw:t.key,value:t.key,architype:"text"},this.writeMemory("recentTarget",t.target),this.writeMemory("interactionDelay",0))}userPasteAction(t){if(t.isTrusted){this.action={type:"paste",architype:"input"};let e="",a="text";t.clipboardData||t.originalEvent.clipboardData?(e=(t.originalEvent||t).clipboardData.getData("text/html"),""==e?e=(t.originalEvent||t).clipboardData.getData("text"):a="text/html"):globalThis.clipboardData&&(e=globalThis.clipboardData.getData("Text"));const r=e;e=e.trim(),e=e.replace(/<span>\s*?<\/span>/g," "),e=e.replace(/(?:style="(\S+:\s*[^;"]+;\s*)*)+"/g,""),e=e.replace(/<div/g,"<p"),e=e.replace(/<\/div>/g,"</p>");let s=e;this.isBase64(e)?(a="base64",s=this.isBase64(e)):t.clipboardData.files.length>0?(a="file",t.clipboardData.files.length>1&&(a="files")):i(e)&&(a="url"),this.data={raw:r,value:s,architype:a}}}userDropAction(t){t.preventDefault(),t.stopPropagation(),t.stopImmediatePropagation(),t.isTrusted&&(t.dataTransfer.items?[...t.dataTransfer.items].forEach(((e,a)=>{if("file"===e.kind){let a=e.getAsFile();this.action={type:"drop",architype:"input"},this.data={event:t,file:a,raw:t.dataTransfer.items[0].type,value:t.dataTransfer.items[0].type,architype:t.dataTransfer.items[0].kind}}})):[...t.dataTransfer.files].forEach(((e,a)=>{this.action={type:"drop",architype:"input"},this.data={event:t,file:e,raw:t.dataTransfer.items[0].type,value:t.dataTransfer.items[0].type,architype:t.dataTransfer.items[0].kind}})))}userDragAction(t){t.isTrusted&&t.dataTransfer&&t.dataTransfer.items&&t.dataTransfer.items.length>0&&(this.action={type:"drag",architype:"input"},this.data={raw:t.dataTransfer.items[0].type,value:t.dataTransfer.items[0].type,architype:t.dataTransfer.items[0].kind})}userMouseAction(t){t.isTrusted&&(this.action={type:"click",architype:"input"},this.writeMemory("recentTarget",t.target),this.writeMemory("interactionDelay",0),this.incrementWriteMemory("interactionCount",1))}incrementWriteMemory(t,e,a="short"){let i="stMemory";"long"==a&&(i="ltMemory"),this.writeMemory(t,this[i][t]+e,a)}writeMemory(e,a,i="short"){let r="stMemory";"long"==i?(r="ltMemory",this[r][e]=a,t(`user-scaffold-${r}`,this[r])):this[r][e]=a}deleteMemory(t,e="short"){let i="stMemory";"long"==e?(i="ltMemory",delete this[i][t],a(`user-scaffold-${i}`)):delete this[i][t]}readMemory(t){return this.memory[t]?c(this.memory[t]):null}get memory(){return{...this.ltMemory,...this.stMemory}}isBase64(t){try{return btoa(atob(t))==t}catch(t){return!1}}disconnectedCallback(){this.windowControllers.abort(),super.disconnectedCallback()}}customElements.define(UserScaffold.tag,UserScaffold);