import t from"/static/logging/index.js";var e="object"==typeof global&&global&&global.Object===Object&&global,n="object"==typeof self&&self&&self.Object===Object&&self,r=e||n||Function("return this")(),o=r.Symbol,i=Object.prototype,s=i.hasOwnProperty,a=i.toString,c=o?o.toStringTag:void 0,l=Object.prototype.toString,u=o?o.toStringTag:void 0;var f=/\s/,d=/^\s+/;function v(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var h=/^[-+]0x[0-9a-f]+$/i,b=/^0b[01]+$/i,p=/^0o[0-7]+$/i,g=parseInt;function m(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return null!=t&&"object"==typeof t}(t)&&"[object Symbol]"==function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":u&&u in Object(t)?function(t){var e=s.call(t,c),n=t[c];try{t[c]=void 0;var r=!0}catch(t){}var o=a.call(t);return r&&(e?t[c]=n:delete t[c]),o}(t):function(t){return l.call(t)}(t)}(t)}(t))return NaN;if(v(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=v(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=function(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&f.test(t.charAt(e)););return e}(t)+1).replace(d,""):t}(t);var n=b.test(t);return n||p.test(t)?g(t.slice(2),n?2:8):h.test(t)?NaN:+t}var y=function(){return r.Date.now()},j=Math.max,w=Math.min;function O(t,e,n){var r,o,i,s,a,c,l=0,u=!1,f=!1,d=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function h(e){var n=r,i=o;return r=o=void 0,l=e,s=t.apply(i,n)}function b(t){return l=t,a=setTimeout(g,e),u?h(t):s}function p(t){var n=t-c;return void 0===c||n>=e||n<0||f&&t-l>=i}function g(){var t=y();if(p(t))return O(t);a=setTimeout(g,function(t){var n=e-(t-c);return f?w(n,i-(t-l)):n}(t))}function O(t){return a=void 0,d&&r?h(t):(r=o=void 0,s)}function x(){var t=y(),n=p(t);if(r=arguments,o=this,c=t,n){if(void 0===a)return b(c);if(f)return clearTimeout(a),a=setTimeout(g,e),h(c)}return void 0===a&&(a=setTimeout(g,e)),s}return e=m(e)||0,v(n)&&(u=!!n.leading,i=(f="maxWait"in n)?j(m(n.maxWait)||0,e):i,d="trailing"in n?!!n.trailing:d),x.cancel=function(){void 0!==a&&clearTimeout(a),l=0,r=c=o=a=void 0},x.flush=function(){return void 0===a?s:O(y())},x}function x(t,e,n){var r=!0,o=!0;if("function"!=typeof t)throw new TypeError("Expected a function");return v(n)&&(r="leading"in n?!!n.leading:r,o="trailing"in n?!!n.trailing:o),O(t,e,{leading:r,maxWait:e,trailing:o})}const T=t.getLogger("@quotquot/events"),$=t=>t.name.slice(8);class E{constructor(t){this.el=t,this.callbacks=new Map}add(t,e,n){const r=[];for(const[o,i]of function*(t){for(const e of t.split(";")){const t=/^\s*(?:\[(debounce|throttle)=(\d+)\])?([\w-]*)\s*$/u.exec(e);if(!t){T.warn(`incorrect event format: "${e}"`);continue}const n=t[1]&&("debounce"===t[1]?e=>O(e,parseInt(t[2],10)):e=>x(e,parseInt(t[2],10)));yield[t[3],n]}}(n)){let n=e[o];n?(T.debug(`binding ${t} to ${o}`),i&&(n=i(n)),r.push(n)):T.warn(`event handler "${o}" not in state`)}for(const e of r)this.el.addEventListener(t,e);this.callbacks.set(t,r)}remove(t){T.debug(`unbinding ${t}`);for(const e of this.callbacks.get(t))this.el.removeEventListener(t,e);this.callbacks.delete(t)}render(t,e,n,r){if(!r||r.includes("events"))for(const t of n){const n=$(t);this.remove(n),this.add(n,e,t.value)}}clear(t){for(const t of Object.keys(this.callbacks))this.remove(t)}isEmpty(){return 0===this.callbacks.size}}class k{constructor(t){this.handlers=new Map,t.renderers.push(this)}_addHandler(t,e,n,r){const o=new E(t);for(const t of r)o.add($(t),n,t.value);return this.handlers.set(t,o),o}_processMutations(t,e){const n=t.state.events;for(const t of e||this.observer.takeRecords()){const e=t.attributeName;if(!e.startsWith("data-on-"))continue;const r=t.target,o=r.getAttributeNode(e),i=$(o);let s=this.handlers.get(r);null===t.oldValue?(o||T.warn(`unexpected: element should have ${e}`),s||(s=this._addHandler(r)),s.add(i,n,o.value)):o?t.oldValue!==o.value&&s.render(i,n,[o]):(s.remove(i),s.isEmpty()&&this.handlers.delete(r))}}async setup(t){this.observer=new MutationObserver((e=>this._processMutations(t,e))),this.observer.observe(t.shadowRoot,{subtree:!0,attributeOldValue:!0})}async cleanup(t){this.observer&&(this.observer.disconnect(),this.observer=null);for(const[t,e]of this.handlers)e.clear(t);this.handlers.clear()}async renderElement(t,e,n,r,o,i){const s=n.events;if(!s)return;const a=[];for(const e of t.attributes)e.name.startsWith("data-on-")&&a.push(e);if(0===a.length)return;const c=this.handlers.get(t);c?c.render(e,s,a,r):this._addHandler(t,e,s,a)}}export{k as default};
