export function getPreviousSib(t,e){let s=t;for(;s&&(s.hasAttribute("on")||null!=e&&!s.matches(e));)s=s.previousElementSibling||t.parentElement;return s}export function nudge(t){const e=t.getAttribute("disabled");null!==e&&(0===e.length||"1"===e?t.removeAttribute("disabled"):t.setAttribute("disabled",(parseInt(e)-1).toString()))}export function getProp(t,e,s){let n=t,r=!0;return e.forEach((t=>{if(n&&""!==t){if(r&&"target"===t&&null===n.target)n=s._trigger;else switch(typeof t){case"string":n=n[t];break;default:n=n[t[0]].apply(n,t[1])}r=!1}})),n}export function convert(t,e){if(null===e)return t;let s=t;switch(e){case"bool":s="true"===t;break;case"int":s=parseInt(t);break;case"float":s=parseFloat(t);break;case"date":s=new Date(t);break;case"truthy":s=!!t;break;case"falsy":s=!t}return s}const ltcRe=/(\-\w)/g;export function lispToCamel(t){return t.replace(ltcRe,(function(t){return t[1].toUpperCase()}))}export function findMatches(t,e,s,n,r){let o=[];e=e||"*";const a=s??1/0;let l,i=0;for(l=n?t.closest(n):t.nextElementSibling;null!==l;){if(l.matches(e)){if(r){const t=Array.from(l.querySelectorAll(r));o=o.concat(t),i+=t.length}else i++,o.push(l);if(i>a)break}l=l.nextElementSibling}return o}export function getToProp(t,e,s){let n=e||t;if(!n)return null;const r=n.lastIndexOf("[");return-1===r?null:(n=n.replace("[data-","[-"),n=n.substring(r+2,n.length-1),s?n:lispToCamel(n))}export function passVal(t,e,s,n,r,o,a,l,i){const c=i??findMatches(e,s,r,o,n);return passValToMatches(c,t,s,n,a,l),c}export function passValToMatches(t,e,s,n,r,o){const a=getToProp(s,n,o),l=!!r&&null!==a,i=l?a:r||a;if(null===i)throw"No to prop.";t.forEach((t=>{const s=l?t[r]:t;switch(o){case"str-attr":s.setAttribute(i,e.toString());break;case"obj-attr":s.setAttribute(i,JSON.stringify(e));break;case"bool-attr":e?s.setAttribute(i,""):s.removeAttribute(i);break;default:s[i]=e}}))}export class OnToMe extends HTMLElement{connectedCallback(){this.style.display="none";const t=this._g=this.getAttribute.bind(this),e=getPreviousSib(this,t("observe"));nudge(e),e.addEventListener(t("on"),(t=>{t.stopPropagation(),this.getVal(t)}));const s=t("mutate-event");null!==s&&this.parentElement?.addEventListener(s,(t=>{this.putVal()}));const n=t("init-val");if(null!==n){this.setInit(e,n);const s=t("init-event");null!==s&&e.addEventListener(s,(t=>{this.setInit(e,n)}),{once:!0})}}setInit(t,e){const s=this._g;let n=getProp(t,e.split("."),this);n=convert(n,s("parse-val-as")),this._lastVal=n;const r=s("me"),o=null===r?1/0:parseInt(r);passVal(n,this,s("to"),s("care-of"),o,s("from"),s("prop"),s("as"))}getVal(t){let e=getProp(t,this._g("val")?.split("."),this);void 0!==e&&(e=convert(e,this._g("parse-val-as")),this._lastVal=e,this.putVal())}putVal(){if(void 0===this._lastVal)return;const t=this._g;this._g("to"),this._g("care-of");passVal(this._lastVal,this,t("to"),t("care-of"),t("me"),t("from"),t("prop"),t("as"))}}const otm="on-to-me";customElements.get(otm)||customElements.define(otm,OnToMe);