!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).FetchTable=t()}(this,function(){"use strict";var e;function t(e,t,n){function i(n,i){if(n._zod||Object.defineProperty(n,"_zod",{value:{def:i,constr:o,traits:new Set},enumerable:!1}),n._zod.traits.has(e))return;n._zod.traits.add(e),t(n,i);const s=o.prototype,a=Object.keys(s);for(let e=0;e<a.length;e++){const t=a[e];t in n||(n[t]=s[t].bind(n))}}const s=n?.Parent??Object;class a extends s{}function o(e){var t;const s=n?.Parent?new a:this;i(s,e),(t=s._zod).deferred??(t.deferred=[]);for(const e of s._zod.deferred)e();return s}return Object.defineProperty(a,"name",{value:e}),Object.defineProperty(o,"init",{value:i}),Object.defineProperty(o,Symbol.hasInstance,{value:t=>!!(n?.Parent&&t instanceof n.Parent)||t?._zod?.traits?.has(e)}),Object.defineProperty(o,"name",{value:e}),o}class n extends Error{constructor(){super("Encountered Promise during synchronous parse. Use .parseAsync() instead.")}}(e=globalThis).__zod_globalConfig??(e.__zod_globalConfig={});const i=globalThis.__zod_globalConfig;function s(e){return i}function a(e,t){return"bigint"==typeof t?t.toString():t}function o(e){const t=e.startsWith("^")?1:0,n=e.endsWith("$")?e.length-1:e.length;return e.slice(t,n)}const r=Symbol("evaluating");function l(e,t,n){let i;Object.defineProperty(e,t,{get(){if(i!==r)return void 0===i&&(i=r,i=n()),i},set(n){Object.defineProperty(e,t,{value:n})},configurable:!0})}const c="captureStackTrace"in Error?Error.captureStackTrace:(...e)=>{};function u(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}function h(e){if(!1===u(e))return!1;const t=e.constructor;if(void 0===t)return!0;if("function"!=typeof t)return!0;const n=t.prototype;return!1!==u(n)&&!1!==Object.prototype.hasOwnProperty.call(n,"isPrototypeOf")}const p=new Set(["string","number","symbol"]);function d(e){const t=e;if(!t)return{};if("string"==typeof t)return{error:()=>t};if(void 0!==t?.message){if(void 0!==t?.error)throw new Error("Cannot specify both `message` and `error` params");t.error=t.message}return delete t.message,"string"==typeof t.error?{...t,error:()=>t.error}:t}function f(e,t=0){if(!0===e.aborted)return!0;for(let n=t;n<e.issues.length;n++)if(!0!==e.issues[n]?.continue)return!0;return!1}function g(e,t=0){if(!0===e.aborted)return!0;for(let n=t;n<e.issues.length;n++)if(!1===e.issues[n]?.continue)return!0;return!1}function m(e,t){return t.map(t=>{var n;return(n=t).path??(n.path=[]),t.path.unshift(e),t})}function _(e){return"string"==typeof e?e:e?.message}function b(e,t,n){const i=e.message?e.message:_(e.inst?._zod.def?.error?.(e))??_(t?.error?.(e))??_(n.customError?.(e))??_(n.localeError?.(e))??"Invalid input",{inst:s,continue:a,input:o,...r}=e;return r.path??(r.path=[]),r.message=i,t?.reportInput&&(r.input=o),r}const v=(e,t)=>{e.name="$ZodError",Object.defineProperty(e,"_zod",{value:e._zod,enumerable:!1}),Object.defineProperty(e,"issues",{value:t,enumerable:!1}),e.message=JSON.stringify(t,a,2),Object.defineProperty(e,"toString",{value:()=>e.message,enumerable:!1})},y=t("$ZodError",v),z=t("$ZodError",v,{Parent:Error}),N=(e=>(t,i,a,o)=>{const r=a?{...a,async:!1}:{async:!1},l=t._zod.run({value:i,issues:[]},r);if(l instanceof Promise)throw new n;if(l.issues.length){const t=new(o?.Err??e)(l.issues.map(e=>b(e,r,s())));throw c(t,o?.callee),t}return l.value})(z),w=(e=>async(t,n,i,a)=>{const o=i?{...i,async:!0}:{async:!0};let r=t._zod.run({value:n,issues:[]},o);if(r instanceof Promise&&(r=await r),r.issues.length){const t=new(a?.Err??e)(r.issues.map(e=>b(e,o,s())));throw c(t,a?.callee),t}return r.value})(z),C=(e=>(t,i,a)=>{const o=a?{...a,async:!1}:{async:!1},r=t._zod.run({value:i,issues:[]},o);if(r instanceof Promise)throw new n;return r.issues.length?{success:!1,error:new(e??y)(r.issues.map(e=>b(e,o,s())))}:{success:!0,data:r.value}})(z),S=(e=>async(t,n,i)=>{const a=i?{...i,async:!0}:{async:!0};let o=t._zod.run({value:n,issues:[]},a);return o instanceof Promise&&(o=await o),o.issues.length?{success:!1,error:new e(o.issues.map(e=>b(e,a,s())))}:{success:!0,data:o.value}})(z),T=/^-?\d+(?:\.\d+)?$/,P=/^(?:true|false)$/i,E={major:4,minor:4,patch:3},L=t("$ZodType",(e,t)=>{var i;e??(e={}),e._zod.def=t,e._zod.bag=e._zod.bag||{},e._zod.version=E;const s=[...e._zod.def.checks??[]];e._zod.traits.has("$ZodCheck")&&s.unshift(e);for(const t of s)for(const n of t._zod.onattach)n(e);if(0===s.length)(i=e._zod).deferred??(i.deferred=[]),e._zod.deferred?.push(()=>{e._zod.run=e._zod.parse});else{const t=(e,t,i)=>{let s,a=f(e);for(const o of t){if(o._zod.def.when){if(g(e))continue;if(!o._zod.def.when(e))continue}else if(a)continue;const t=e.issues.length,r=o._zod.check(e);if(r instanceof Promise&&!1===i?.async)throw new n;if(s||r instanceof Promise)s=(s??Promise.resolve()).then(async()=>{await r;e.issues.length!==t&&(a||(a=f(e,t)))});else{if(e.issues.length===t)continue;a||(a=f(e,t))}}return s?s.then(()=>e):e},i=(i,a,o)=>{if(f(i))return i.aborted=!0,i;const r=t(a,s,o);if(r instanceof Promise){if(!1===o.async)throw new n;return r.then(t=>e._zod.parse(t,o))}return e._zod.parse(r,o)};e._zod.run=(a,o)=>{if(o.skipChecks)return e._zod.parse(a,o);if("backward"===o.direction){const t=e._zod.parse({value:a.value,issues:[]},{...o,skipChecks:!0});return t instanceof Promise?t.then(e=>i(e,a,o)):i(t,a,o)}const r=e._zod.parse(a,o);if(r instanceof Promise){if(!1===o.async)throw new n;return r.then(e=>t(e,s,o))}return t(r,s,o)}}l(e,"~standard",()=>({validate:t=>{try{const n=C(e,t);return n.success?{value:n.data}:{issues:n.error?.issues}}catch(n){return S(e,t).then(e=>e.success?{value:e.data}:{issues:e.error?.issues})}},vendor:"zod",version:1}))}),j=t("$ZodString",(e,t)=>{var n;L.init(e,t),e._zod.pattern=[...e?._zod.bag?.patterns??[]].pop()??(n=e._zod.bag,new RegExp(`^${n?`[\\s\\S]{${n?.minimum??0},${n?.maximum??""}}`:"[\\s\\S]*"}$`)),e._zod.parse=(n,i)=>{if(t.coerce)try{n.value=String(n.value)}catch(i){}return"string"==typeof n.value||n.issues.push({expected:"string",code:"invalid_type",input:n.value,inst:e}),n}}),k=t("$ZodNumber",(e,t)=>{L.init(e,t),e._zod.pattern=e._zod.bag.pattern??T,e._zod.parse=(n,i)=>{if(t.coerce)try{n.value=Number(n.value)}catch(e){}const s=n.value;if("number"==typeof s&&!Number.isNaN(s)&&Number.isFinite(s))return n;const a="number"==typeof s?Number.isNaN(s)?"NaN":Number.isFinite(s)?void 0:"Infinity":void 0;return n.issues.push({expected:"number",code:"invalid_type",input:s,inst:e,...a?{received:a}:{}}),n}}),x=t("$ZodBoolean",(e,t)=>{L.init(e,t),e._zod.pattern=P,e._zod.parse=(n,i)=>{if(t.coerce)try{n.value=Boolean(n.value)}catch(e){}const s=n.value;return"boolean"==typeof s||n.issues.push({expected:"boolean",code:"invalid_type",input:s,inst:e}),n}});function M(e,t,n){e.issues.length&&t.issues.push(...m(n,e.issues)),t.value[n]=e.value}const D=t("$ZodArray",(e,t)=>{L.init(e,t),e._zod.parse=(n,i)=>{const s=n.value;if(!Array.isArray(s))return n.issues.push({expected:"array",code:"invalid_type",input:s,inst:e}),n;n.value=Array(s.length);const a=[];for(let e=0;e<s.length;e++){const o=s[e],r=t.element._zod.run({value:o,issues:[]},i);r instanceof Promise?a.push(r.then(t=>M(t,n,e))):M(r,n,e)}return a.length?Promise.all(a).then(()=>n):n}});function O(e,t,n,i,s,a){const o=n in i;if(e.issues.length){if(s&&a&&!o)return;t.issues.push(...m(n,e.issues))}o||s?void 0===e.value?o&&(t.value[n]=void 0):t.value[n]=e.value:e.issues.length||t.issues.push({code:"invalid_type",expected:"nonoptional",input:void 0,path:[n]})}function $(e){const t=Object.keys(e.shape);for(const n of t)if(!e.shape?.[n]?._zod?.traits?.has("$ZodType"))throw new Error(`Invalid element at key "${n}": expected a Zod schema`);const n=(i=e.shape,Object.keys(i).filter(e=>"optional"===i[e]._zod.optin&&"optional"===i[e]._zod.optout));var i;return{...e,keys:t,keySet:new Set(t),numKeys:t.length,optionalKeys:new Set(n)}}const R=t("$ZodObject",(e,t)=>{L.init(e,t);const n=Object.getOwnPropertyDescriptor(t,"shape");if(!n?.get){const e=t.shape;Object.defineProperty(t,"shape",{get:()=>{const n={...e};return Object.defineProperty(t,"shape",{value:n}),n}})}const i=(s=()=>$(t),{get value(){{const e=s();return Object.defineProperty(this,"value",{value:e}),e}}});var s;l(e._zod,"propValues",()=>{const e=t.shape,n={};for(const t in e){const i=e[t]._zod;if(i.values){n[t]??(n[t]=new Set);for(const e of i.values)n[t].add(e)}}return n});const a=u,o=t.catchall;let r;e._zod.parse=(t,n)=>{r??(r=i.value);const s=t.value;if(!a(s))return t.issues.push({expected:"object",code:"invalid_type",input:s,inst:e}),t;t.value={};const l=[],c=r.shape;for(const e of r.keys){const i=c[e],a="optional"===i._zod.optin,o="optional"===i._zod.optout,r=i._zod.run({value:s[e],issues:[]},n);r instanceof Promise?l.push(r.then(n=>O(n,t,e,s,a,o))):O(r,t,e,s,a,o)}return o?function(e,t,n,i,s,a){const o=[],r=s.keySet,l=s.catchall._zod,c=l.def.type,u="optional"===l.optin,h="optional"===l.optout;for(const s in t){if("__proto__"===s)continue;if(r.has(s))continue;if("never"===c){o.push(s);continue}const a=l.run({value:t[s],issues:[]},i);a instanceof Promise?e.push(a.then(e=>O(e,n,s,t,u,h))):O(a,n,s,t,u,h)}return o.length&&n.issues.push({code:"unrecognized_keys",keys:o,input:t,inst:a}),e.length?Promise.all(e).then(()=>n):n}(l,s,t,n,i.value,e):l.length?Promise.all(l).then(()=>t):t}}),A=t("$ZodRecord",(e,t)=>{L.init(e,t),e._zod.parse=(n,i)=>{const a=n.value;if(!h(a))return n.issues.push({expected:"record",code:"invalid_type",input:a,inst:e}),n;const o=[],r=t.keyType._zod.values;if(r){n.value={};const l=new Set;for(const c of r)if("string"==typeof c||"number"==typeof c||"symbol"==typeof c){l.add("number"==typeof c?c.toString():c);const r=t.keyType._zod.run({value:c,issues:[]},i);if(r instanceof Promise)throw new Error("Async schemas not supported in object keys currently");if(r.issues.length){n.issues.push({code:"invalid_key",origin:"record",issues:r.issues.map(e=>b(e,i,s())),input:c,path:[c],inst:e});continue}const u=r.value,h=t.valueType._zod.run({value:a[c],issues:[]},i);h instanceof Promise?o.push(h.then(e=>{e.issues.length&&n.issues.push(...m(c,e.issues)),n.value[u]=e.value})):(h.issues.length&&n.issues.push(...m(c,h.issues)),n.value[u]=h.value)}let c;for(const e in a)l.has(e)||(c=c??[],c.push(e));c&&c.length>0&&n.issues.push({code:"unrecognized_keys",input:a,inst:e,keys:c})}else{n.value={};for(const r of Reflect.ownKeys(a)){if("__proto__"===r)continue;if(!Object.prototype.propertyIsEnumerable.call(a,r))continue;let l=t.keyType._zod.run({value:r,issues:[]},i);if(l instanceof Promise)throw new Error("Async schemas not supported in object keys currently");if("string"==typeof r&&T.test(r)&&l.issues.length){const e=t.keyType._zod.run({value:Number(r),issues:[]},i);if(e instanceof Promise)throw new Error("Async schemas not supported in object keys currently");0===e.issues.length&&(l=e)}if(l.issues.length){"loose"===t.mode?n.value[r]=a[r]:n.issues.push({code:"invalid_key",origin:"record",issues:l.issues.map(e=>b(e,i,s())),input:r,path:[r],inst:e});continue}const c=t.valueType._zod.run({value:a[r],issues:[]},i);c instanceof Promise?o.push(c.then(e=>{e.issues.length&&n.issues.push(...m(r,e.issues)),n.value[l.value]=e.value})):(c.issues.length&&n.issues.push(...m(r,c.issues)),n.value[l.value]=c.value)}}return o.length?Promise.all(o).then(()=>n):n}}),H=t("$ZodEnum",(e,t)=>{L.init(e,t);const n=function(e){const t=Object.values(e).filter(e=>"number"==typeof e);return Object.entries(e).filter(([e,n])=>-1===t.indexOf(+e)).map(([e,t])=>t)}(t.entries),i=new Set(n);e._zod.values=i,e._zod.pattern=new RegExp(`^(${n.filter(e=>p.has(typeof e)).map(e=>"string"==typeof e?e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):e.toString()).join("|")})$`),e._zod.parse=(t,s)=>{const a=t.value;return i.has(a)||t.issues.push({code:"invalid_value",values:n,input:a,inst:e}),t}});function Z(e,t){return void 0===t&&(e.issues.length||e.fallback)?{issues:[],value:void 0}:e}const B=t("$ZodOptional",(e,t)=>{L.init(e,t),e._zod.optin="optional",e._zod.optout="optional",l(e._zod,"values",()=>t.innerType._zod.values?new Set([...t.innerType._zod.values,void 0]):void 0),l(e._zod,"pattern",()=>{const e=t.innerType._zod.pattern;return e?new RegExp(`^(${o(e.source)})?$`):void 0}),e._zod.parse=(e,n)=>{if("optional"===t.innerType._zod.optin){const i=e.value,s=t.innerType._zod.run(e,n);return s instanceof Promise?s.then(e=>Z(e,i)):Z(s,i)}return void 0===e.value?e:t.innerType._zod.run(e,n)}}),q=t("$ZodNullable",(e,t)=>{L.init(e,t),l(e._zod,"optin",()=>t.innerType._zod.optin),l(e._zod,"optout",()=>t.innerType._zod.optout),l(e._zod,"pattern",()=>{const e=t.innerType._zod.pattern;return e?new RegExp(`^(${o(e.source)}|null)$`):void 0}),l(e._zod,"values",()=>t.innerType._zod.values?new Set([...t.innerType._zod.values,null]):void 0),e._zod.parse=(e,n)=>null===e.value?e:t.innerType._zod.run(e,n)}),F=t("$ZodDefault",(e,t)=>{L.init(e,t),e._zod.optin="optional",l(e._zod,"values",()=>t.innerType._zod.values),e._zod.parse=(e,n)=>{if("backward"===n.direction)return t.innerType._zod.run(e,n);if(void 0===e.value)return e.value=t.defaultValue,e;const i=t.innerType._zod.run(e,n);return i instanceof Promise?i.then(e=>Q(e,t)):Q(i,t)}});function Q(e,t){return void 0===e.value&&(e.value=t.defaultValue),e}const I=t("ZodMiniType",(e,t)=>{if(!e._zod)throw new Error("Uninitialized schema in ZodMiniType.");L.init(e,t),e.def=t,e.type=t.type,e.parse=(t,n)=>N(e,t,n,{callee:e.parse}),e.safeParse=(t,n)=>C(e,t,n),e.parseAsync=async(t,n)=>w(e,t,n,{callee:e.parseAsync}),e.safeParseAsync=async(t,n)=>S(e,t,n),e.check=(...n)=>e.clone({...t,checks:[...t.checks??[],...n.map(e=>"function"==typeof e?{_zod:{check:e,def:{check:"custom"},onattach:[]}}:e)]},{parent:!0}),e.with=e.check,e.clone=(t,n)=>function(e,t,n){const i=new e._zod.constr(t??e._zod.def);return t&&!n?.parent||(i._zod.parent=e),i}(e,t,n),e.brand=()=>e,e.register=(t,n)=>(t.add(e,n),e),e.apply=t=>t(e)}),U=t("ZodMiniString",(e,t)=>{j.init(e,t),I.init(e,t)});function G(e){return function(e,t){return new e({type:"string",...d(t)})}(U,e)}const V=t("ZodMiniNumber",(e,t)=>{k.init(e,t),I.init(e,t)});function K(e){return function(e,t){return new e({type:"number",checks:[],...d(t)})}(V,e)}const W=t("ZodMiniBoolean",(e,t)=>{x.init(e,t),I.init(e,t)});function X(e){return function(e,t){return new e({type:"boolean",...d(t)})}(W,e)}const J=t("ZodMiniArray",(e,t)=>{D.init(e,t),I.init(e,t)});function Y(e,t){return new J({type:"array",element:e,...d(t)})}const ee=t("ZodMiniObject",(e,t)=>{R.init(e,t),I.init(e,t),l(e,"shape",()=>t.shape)});function te(e,t){const n={type:"object",shape:e??{},...d(t)};return new ee(n)}const ne=t("ZodMiniRecord",(e,t)=>{A.init(e,t),I.init(e,t)});function ie(e,t,n){return t&&t._zod?new ne({type:"record",keyType:e,valueType:t,...d(n)}):new ne({type:"record",keyType:G(),valueType:e,...d(t)})}const se=t("ZodMiniEnum",(e,t)=>{H.init(e,t),I.init(e,t),e.options=Object.values(t.entries)});function ae(e,t){const n=Array.isArray(e)?Object.fromEntries(e.map(e=>[e,e])):e;return new se({type:"enum",entries:n,...d(t)})}const oe=t("ZodMiniOptional",(e,t)=>{B.init(e,t),I.init(e,t)});function re(e){return new oe({type:"optional",innerType:e})}const le=t("ZodMiniNullable",(e,t)=>{q.init(e,t),I.init(e,t)});const ce=t("ZodMiniDefault",(e,t)=>{F.init(e,t),I.init(e,t)});function ue(e,t){return new ce({type:"default",innerType:e,get defaultValue(){return"function"==typeof t?t():h(e=t)?{...e}:Array.isArray(e)?[...e]:e instanceof Map?new Map(e):e instanceof Set?new Set(e):e;var e}})}const he=te({type:ae(["text","html"]),label:G(),className:re(G()),sortable:X(),searchable:X()}),pe=te({ajaxURL:G(),ajaxMethod:ae(["GET","POST"]),ajaxHeaders:ue(ie(G(),G()),{"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest","X-Requested-By":"fetch-table"}),debug:ue(X(),!1),columns:ie(G(),he),elements:re(te({container:re(te({container:re(te({className:re(G()),querySelector:re(G()),attributes:re(ie(G(),G()))})),header:re(te({className:re(G()),querySelector:re(G()),attributes:re(ie(G(),G()))})),footer:re(te({className:re(G()),querySelector:re(G()),attributes:re(ie(G(),G()))}))})),table:re(te({table:re(te({className:re(G()),attributes:re(ie(G(),G()))})),tableHead:re(te({tableHead:re(te({className:re(G()),attributes:re(ie(G(),G()))})),tableRow:re(te({className:re(G()),attributes:re(ie(G(),G()))})),tableCell:re(te({className:re(G()),attributes:re(ie(G(),G()))}))})),tableBody:re(te({tableBody:re(te({className:re(G()),attributes:re(ie(G(),G()))})),tableRow:re(te({className:re(G()),attributes:re(ie(G(),G()))})),tableCell:re(te({className:re(G()),attributes:re(ie(G(),G()))}))})),placeholder:re(te({className:re(G()),innerHTML:re(G()),attributes:re(ie(G(),G()))}))})),pagination:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))})),button:re(te({primary:re(te({className:re(G()),attributes:re(ie(G(),G()))})),active:re(te({className:re(G()),attributes:re(ie(G(),G()))})),ellipsis:re(te({className:re(G()),innerHTML:re(G()),attributes:re(ie(G(),G()))})),previous:re(te({className:re(G()),innerHTML:re(G()),attributes:re(ie(G(),G()))})),next:re(te({className:re(G()),innerHTML:re(G()),attributes:re(ie(G(),G()))}))})),sizeSelector:re(te({container:re(te({className:re(G()),innerHTML:re(G()),attributes:re(ie(G(),G()))})),select:re(te({className:re(G()),attributes:re(ie(G(),G()))})),option:re(te({className:re(G()),attributes:re(ie(G(),G()))}))})),description:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))}))}))})),search:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))})),input:re(te({className:re(G()),attributes:re(ie(G(),G()))}))})),filter:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))})),filter:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))})),label:re(te({className:re(G()),attributes:re(ie(G(),G()))})),select:re(te({select:re(te({className:re(G()),attributes:re(ie(G(),G()))})),option:re(te({className:re(G()),attributes:re(ie(G(),G()))}))})),button:re(te({container:re(te({className:re(G()),attributes:re(ie(G(),G()))})),button:re(te({default:re(te({className:re(G()),attributes:re(ie(G(),G()))})),active:re(te({className:re(G()),attributes:re(ie(G(),G()))}))}))}))}))}))})),components:te({search:te({active:X()}),filter:re(te({active:X(),allLabel:re(G()),filters:Y(te({columnName:G(),label:re(G()),type:ae(["button","select"]),values:Y((de=G(),new le({type:"nullable",innerType:de})))}))})),pagination:te({active:X(),pageSize:K(),availableSizes:Y(K()),style:ae(["standard","simple"]),description:re(te({active:X(),innerHTML:re(G())}))})})});var de;function fe(e,t={}){const n=document.createElement(e);return Object.entries(t).forEach(([e,t])=>{null!=t&&("attributes"===e?Object.entries(t).forEach(([e,t])=>{n.setAttribute(e,String(t))}):e in n?n[e]=t:n.setAttribute(e,String(t)))}),n}class ge{constructor(){this._handlers={}}register(e,t,n=1e3){this._handlers[e]||(this._handlers[e]=[]),this._handlers[e].push({callback:t,priority:n})}dispatch(e,t){this._handlers[e]?.sort((e,t)=>e.priority-t.priority).forEach(e=>{e.callback(t)})}}const me=te({data:Y(Y(te({column:G(),className:re(G()),value:G()}))),pagination:re(te({page:K(),pageSize:K(),totalPages:K()})),totalRecords:K()});class _e{constructor(e,t){this._sort=null,this._pagination=null,this._searchQuery=null,this._filter=[],this._config=e,this._eventDispatcher=t}refresh(){this._config.debug&&console.info("Refreshing data..");const e=this.generateRequest();this._eventDispatcher.dispatch("before-data-fetch",e),fetch(e).then(e=>{e.ok&&e.json().then(e=>{const t=me.parse(e);this._eventDispatcher.dispatch("data-fetch",t),this._eventDispatcher.dispatch("after-data-fetch")}).catch(e=>{this._config.debug&&console.error(e),this._eventDispatcher.dispatch("data-fetch-error",{error:e}),this._eventDispatcher.dispatch("after-data-fetch")})}).catch(e=>{this._config.debug&&console.error(e),this._eventDispatcher.dispatch("data-fetch-error",{error:e}),this._eventDispatcher.dispatch("after-data-fetch")})}get sort(){return this._sort}set sort(e){this._sort=e}get pagination(){return this._pagination}set pagination(e){this._pagination=e}get searchQuery(){return this._searchQuery}set searchQuery(e){this._searchQuery=e}get filters(){return this._filter}addFilter(e,t=!1){if(!Object.entries(this._config.columns).find(([t,n])=>t===e.columnName))throw new Error(`Column '${e.columnName}' does not exist in the table configuration.`);if(this._filter.find(t=>t.columnName===e.columnName)){if(!t)throw new Error(`Filter for column '${e.columnName}' already exists.`);{const t=this._filter.findIndex(t=>t.columnName===e.columnName);this._filter.splice(t,1,e)}}else this._filter.push(e)}removeFilter(e){const t=this._filter.findIndex(t=>t.columnName===e);-1!==t&&this._filter.splice(t,1)}generateRequest(){const e="GET"===this._config.ajaxMethod?this._config.ajaxURL+"?"+this.generateURLSearchParams():this._config.ajaxURL,t="POST"===this._config.ajaxMethod?JSON.stringify(this.generateRequestBody()):null;return new Request(e,{method:this._config.ajaxMethod,headers:this._config.ajaxHeaders,body:t})}generateRequestBody(){let e={};return null!==this._searchQuery&&""!==this._searchQuery&&(e={...e,search:this._searchQuery}),null!==this._pagination&&(e={...e,pagination:{page:null!==this._searchQuery&&""!==this._searchQuery?1:this._pagination.page,size:this._pagination.pageSize}}),null!==this._sort&&(e={...e,sort:{column:this._sort.columnName,direction:this._sort.direction}}),this._filter.length>0&&(e={...e,filter:this._filter}),e}generateURLSearchParams(){let e=new URLSearchParams;return null!==this._searchQuery&&e.append("search",this._searchQuery),null!==this._pagination&&(e.append("pagination[page]",null!==this._searchQuery&&""!==this._searchQuery?"1":this._pagination.page.toString()),e.append("pagination[size]",this._pagination.pageSize.toString())),null!==this._sort&&(e.append("sort[column]",this._sort.columnName),e.append("sort[direction]",this._sort.direction)),this._filter.length>0&&this._filter.forEach(t=>{e.append("filter",`${t.columnName}=${t.value}`)}),e}}class be{constructor(e,t,n,i){this._isLoading=!1,this._config=t,this._coreElement=e,this._eventDispatcher=n,this._client=i,this._eventDispatcher.register("before-data-fetch",()=>this._isLoading=!0),this._eventDispatcher.register("after-data-fetch",()=>this._isLoading=!1)}}class ve extends be{constructor(e,t,n,i){super(e,t,n,i),this._sort=null,this._elements={table:null,head:null,body:null},this._eventDispatcher.register("data-fetch",e=>this.render(e)),this.init()}init(){this._config.debug&&console.info("[Table Component] Initializing.."),this._coreElement.innerHTML="",this._elements.table=fe("table",{className:this._config.elements?.table?.table?.className,attributes:this._config.elements?.table?.table?.attributes}),this._elements.head=fe("thead",{className:this._config.elements?.table?.tableHead?.tableHead?.className,attributes:this._config.elements?.table?.tableHead?.tableHead?.attributes}),this._elements.body=fe("tbody",{className:this._config.elements?.table?.tableBody?.tableBody?.className,attributes:this._config.elements?.table?.tableBody?.tableBody?.attributes});const e=fe("tr",{className:this._config.elements?.table?.tableHead?.tableRow?.className,attributes:this._config.elements?.table?.tableHead?.tableRow?.attributes});for(const[t,n]of Object.entries(this._config.columns)){const i=fe("th",{scope:"col",className:n.className||this._config.elements?.table?.tableHead?.tableCell?.className,attributes:this._config.elements?.table?.tableHead?.tableCell?.attributes,innerHTML:n.label});n.sortable&&i.addEventListener("click",()=>{this._isLoading||(this._config.debug&&console.info(`[Table Component] Sort by the column: ${t}`),null===this._sort?this._sort={columnName:t,direction:"ASC"}:this._sort.columnName===t?this._sort.direction="ASC"===this._sort.direction?"DESC":"ASC":this._sort={columnName:t,direction:"ASC"},this._eventDispatcher.dispatch("sort-change",this._sort),this._client.sort=this._sort,this._client.refresh())}),e.appendChild(i)}this._elements.head.appendChild(e),this._elements.table.appendChild(this._elements.head),this._elements.table.appendChild(this._elements.body),this._coreElement.appendChild(this._elements.table)}render(e){if(this._config.debug&&console.info("[Table Component] Rendering data.."),null===this._elements.body)throw new Error("[Table Component] Body element couldn't be found. First, initialize the component with the init() method");if(this._elements.body.innerHTML="",0===e.data.length){const e=fe("tr",{className:this._config.elements?.table?.tableBody?.tableRow?.className,attributes:this._config.elements?.table?.tableBody?.tableRow?.attributes}),t=fe("td",{colSpan:this._config.columns.length,className:this._config.elements?.table?.placeholder?.className||this._config.elements?.table?.tableBody?.tableCell?.className,attributes:this._config.elements?.table?.placeholder?.attributes,innerHTML:this._config.elements?.table?.placeholder?.innerHTML});e.appendChild(t),this._elements.body?.appendChild(e)}else e.data.forEach(e=>{const t=fe("tr",{className:this._config.elements?.table?.tableBody?.tableRow?.className,attributes:this._config.elements?.table?.tableBody?.tableRow?.attributes});for(const[n,i]of Object.entries(this._config.columns)){const s=e.find(function(e){return e.column===n});if(void 0===s)throw new Error(`[Table Component] Column ${n} not found`);const a=fe("td",{className:s.className||this._config.elements?.table?.tableBody?.tableCell?.className,attributes:this._config.elements?.table?.tableBody?.tableCell?.attributes});"text"===i.type?a.innerText=s.value:"html"===i.type&&(a.innerHTML=s.value),t.appendChild(a)}this._elements.body?.appendChild(t)})}}class ye extends be{constructor(e,t,n,i){super(e,t,n,i),this._elements={container:null,descriptionContainer:null,sizeContainer:null,sizeSelect:null},this._eventDispatcher.register("data-fetch",e=>this.renderPagination(e)),this._eventDispatcher.register("data-fetch",e=>this.renderDescription(e)),this._eventDispatcher.register("filter-value-change",e=>this.resetCurrentPage()),this.init()}init(){this._config.debug&&console.info("[Pagination Component] Initializing.."),this._coreElement.innerHTML="",this._elements.container=fe("nav",{className:this._config.elements?.pagination?.container?.className,attributes:this._config.elements?.pagination?.container?.attributes,ariaLabel:"Pagination"}),this._elements.descriptionContainer=fe("div",{className:this._config.elements?.pagination?.description?.container?.className,attributes:this._config.elements?.pagination?.description?.container?.attributes}),this._elements.sizeContainer=fe("div",{className:this._config.elements?.pagination?.sizeSelector?.container?.className,attributes:this._config.elements?.pagination?.sizeSelector?.container?.attributes});const e=fe("select",{name:"ft-size-selector",className:this._config.elements?.pagination?.sizeSelector?.select?.className,attributes:this._config.elements?.pagination?.sizeSelector?.select?.attributes});this._config.components?.pagination?.availableSizes?.forEach(t=>{const n=fe("option",{value:t,innerText:t.toString(),selected:t===this._client.pagination?.pageSize?"selected":null,className:this._config.elements?.pagination?.sizeSelector?.option?.className,attributes:this._config.elements?.pagination?.sizeSelector?.option?.attributes});e.appendChild(n)}),e.addEventListener("change",()=>{if(!this._isLoading){this._config.debug&&console.info(`[Pagination Component] Changing page size to ${e.value}`);let t={page:1,pageSize:parseInt(e.value)};this._eventDispatcher.dispatch("pagination-size-change",t),this._client.pagination=t,this._client.refresh()}}),this._elements.sizeContainer.innerHTML=this._config.elements?.pagination?.sizeSelector?.container?.innerHTML??"",this._elements.sizeContainer.appendChild(e),this._coreElement.appendChild(this._elements.descriptionContainer),this._coreElement.appendChild(this._elements.container),this._coreElement.appendChild(this._elements.sizeContainer)}renderPagination(e){if(this._config.debug&&console.info("[Pagination Component] Rendering data.."),null===this._elements.container)throw new Error("[Pagination Component] Container element couldn't be found. First, initialize the component with the init() method");const t=this.getPaginationData(e);this._elements.container.innerText="";const n=fe("button",{className:this._config.elements?.pagination?.button?.previous?.className||this._config.elements?.pagination?.button?.primary?.className,attributes:this._config.elements?.pagination?.button?.previous?.attributes,innerHTML:this._config.elements?.pagination?.button?.previous?.innerHTML,disabled:1===t.page?"disabled":null,type:"button"});n.addEventListener("click",()=>{if(!this._isLoading&&t.page>1){this._config.debug&&console.info("[Pagination Component] Moving to the previous page");let e={page:t.page-1,pageSize:t.pageSize};this._eventDispatcher.dispatch("pagination-change",e),this._client.pagination=e,this._client.refresh()}});const i=fe("button",{type:"button",className:this._config.elements?.pagination?.button?.next?.className||this._config.elements?.pagination?.button?.primary?.className,attributes:this._config.elements?.pagination?.button?.next?.attributes,innerHTML:this._config.elements?.pagination?.button?.next?.innerHTML,disabled:t.page===t.totalPages?"disabled":null});if(i.addEventListener("click",()=>{if(!this._isLoading&&t.page<t.totalPages){this._config.debug&&console.info("[Pagination Component] Moving to the next page");let e={page:t.page+1,pageSize:t.pageSize};this._eventDispatcher.dispatch("pagination-change",e),this._client.pagination=e,this._client.refresh()}}),this._elements.container?.appendChild(n),"simple"!==this._config.components?.pagination?.style){const e=(e,n=!1)=>{const i=fe("button",{className:n&&this._config.elements?.pagination?.button?.active?.className||this._config.elements?.pagination?.button?.primary?.className,attributes:this._config.elements?.pagination?.button?.active?.attributes,innerText:e.toString(),type:"button"});return i.addEventListener("click",()=>{if(!this._isLoading){this._config.debug&&console.info(`[Pagination Component] Moving to page number: ${e}`);let n={page:e,pageSize:t.pageSize};this._eventDispatcher.dispatch("pagination-change",n),this._client.pagination={page:e,pageSize:t.pageSize},this._client.refresh()}}),i},n=()=>fe("span",{className:this._config.elements?.pagination?.button?.ellipsis?.className||this._config.elements?.pagination?.button?.primary?.className,attributes:this._config.elements?.pagination?.button?.previous?.attributes,innerHTML:this._config.elements?.pagination?.button?.ellipsis?.innerHTML||".."}),i=e(1,1===t.page);if(this._elements.container?.appendChild(i),t.page>4&&this._elements.container?.appendChild(n()),t.page<5)for(let n=2;n<=Math.min(5,t.totalPages-1);n++){const i=e(n,n===t.page);this._elements.container?.appendChild(i)}else if(t.page>t.totalPages-4)for(let n=Math.max(t.totalPages-4,2);n<=t.totalPages-1;n++){const i=e(n,n===t.page);this._elements.container?.appendChild(i)}else for(let n=t.page-1;n<=t.page+1;n++){const i=e(n,n===t.page);this._elements.container?.appendChild(i)}if(t.page<=t.totalPages-4&&this._elements.container?.appendChild(n()),t.totalPages>1){const n=e(t.totalPages,t.totalPages===t.page);this._elements.container?.appendChild(n)}}this._elements.container?.appendChild(i)}renderDescription(e){if(this._config.debug&&console.info("[Pagination Component] Rendering data.."),null===this._elements.descriptionContainer)throw new Error("[Pagination Component] Description container element couldn't be found. First, initialize the component with the init() method");if(!0===this._config.components.pagination.description?.active){const t=this.getPaginationData(e);let n=this._config.components.pagination.description?.innerHTML??"Showing _START_ - _END_ of _TOTAL_ entries (Page _PAGE_CURRENT_ of _PAGE_TOTAL_)";n=n.replace("_START_",String(t.page*t.pageSize-t.pageSize+1)),n=n.replace("_END_",String(Math.min(t.page*t.pageSize,e.totalRecords))),n=n.replace("_TOTAL_",String(e.totalRecords)),n=n.replace("_PAGE_CURRENT_",String(t.page)),n=n.replace("_PAGE_TOTAL_",String(t.totalPages)),this._elements.descriptionContainer.innerHTML=n}}getPaginationData(e){if(void 0===e.pagination)throw new Error("[Pagination Component] Pagination data is missing. Please check your API response");return e.pagination}resetCurrentPage(){let e=this._client.pagination;null!==e&&(this._client.pagination={page:1,pageSize:e.pageSize})}}class ze extends be{constructor(e,t,n,i){super(e,t,n,i),this.init()}init(){this._config.debug&&console.info("[Search Component] Initializing.."),this._coreElement.innerHTML="";const e=fe("div",{className:this._config.elements?.search?.container?.className,attributes:this._config.elements?.search?.container?.attributes}),t=fe("input",{type:"text",className:this._config.elements?.search?.input?.className,attributes:this._config.elements?.search?.input?.attributes});let n=null;t.addEventListener("keyup",()=>{this._isLoading||(clearTimeout(n),n=setTimeout(()=>{this._config.debug&&console.info(`[Search Component] Searching for "${t.value}"`);let e=t.value;this._eventDispatcher.dispatch("search-change",e),this._client.searchQuery=e,this._client.refresh()},500))}),e.appendChild(t),this._coreElement.appendChild(e)}}class Ne extends be{constructor(e,t,n,i){super(e,t,n,i),this.init()}init(){this._config.debug&&console.info("[Filter Component] Initializing..");const e=fe("div",{className:this._config.elements?.filter?.container?.className,attributes:this._config.elements?.filter?.container?.attributes});this._config.components.filter?.filters.forEach(t=>{const n=fe("div",{className:this._config.elements?.filter?.filter?.container?.className,attributes:this._config.elements?.filter?.filter?.container?.attributes});if(n.appendChild(fe("span",{innerHTML:t.label??t.columnName,className:this._config.elements?.filter?.filter?.label?.className,attributes:this._config.elements?.filter?.filter?.label?.attributes})),"select"===t.type){const e=fe("select",{name:`ft-filter-${t.columnName}`,className:this._config.elements?.filter?.filter?.select?.select?.className,attributes:this._config.elements?.filter?.filter?.select?.select?.attributes});t.values.includes(null)||t.values.unshift(null),t.values.forEach(t=>{e.appendChild(fe("option",{value:t||"ALL",innerText:t||this._config.components.filter?.allLabel,className:this._config.elements?.filter?.filter?.select?.option?.className,attributes:this._config.elements?.filter?.filter?.select?.option?.attributes}))}),e.addEventListener("change",()=>{if(!this._isLoading){if(this._config.debug&&console.info(`[Filter Component] Changing filter value of column ${t.columnName} to ${e.value}`),"ALL"===e.value)this._eventDispatcher.dispatch("filter-value-change",null),this._client.removeFilter(t.columnName);else{const n={columnName:t.columnName,value:e.value};this._eventDispatcher.dispatch("filter-value-change",n),this._client.addFilter(n,!0),this._client.refresh()}this._client.refresh()}}),n.appendChild(e)}else{const e=fe("div",{className:this._config.elements?.filter?.filter?.button?.container?.className,attributes:this._config.elements?.filter?.filter?.button?.container?.attributes});t.values.includes(null)||t.values.unshift(null),t.values.forEach(n=>{const i=fe("button",{type:"button",innerText:n||this._config.components.filter?.allLabel,className:null===n?this._config.elements?.filter?.filter?.button?.button?.active?.className:this._config.elements?.filter?.filter?.button?.button?.default?.className,attributes:null===n?this._config.elements?.filter?.filter?.button?.button?.active?.attributes:this._config.elements?.filter?.filter?.button?.button?.default?.attributes});i.addEventListener("click",()=>{if(!this._isLoading){this._config.debug&&console.info(`[Filter Component] Changing filter value of column ${t.columnName} to ${n}`);if(e.querySelectorAll("button").forEach(e=>{e.className=this._config.elements?.filter?.filter?.button?.button?.default?.className??"",Object.entries(this._config.elements?.filter?.filter?.button?.button?.default?.attributes??{}).forEach(([t,n])=>{e.removeAttribute(t)})}),i.className=this._config.elements?.filter?.filter?.button?.button?.active?.className??"",Object.entries(this._config.elements?.filter?.filter?.button?.button?.active?.attributes??{}).forEach(([e,t])=>{i.setAttribute(e,t)}),null===n)this._eventDispatcher.dispatch("filter-value-change",null),this._client.removeFilter(t.columnName),this._client.refresh();else{const e={columnName:t.columnName,value:n};this._eventDispatcher.dispatch("filter-value-change",e),this._client.addFilter(e,!0),this._client.refresh()}}}),e.appendChild(i)}),n.appendChild(e)}e.appendChild(n)}),this._coreElement.appendChild(e)}}return class{constructor(e,t){if(this._components={table:null,pagination:null,search:null,filter:null},this._config=this.validateConfig(t),this._coreElement=document.querySelector(e),!this._coreElement)throw new Error("Container element couldn't be found.");this._coreElement.innerHTML="";let n=this.selectElement(this._config.elements?.container?.header?.querySelector);null===n&&(n=fe("div",{className:this._config.elements?.container?.header?.className,attributes:this._config.elements?.container?.header?.attributes}),this._coreElement.appendChild(n));let i=this.selectElement(this._config.elements?.container?.container?.querySelector);null===i&&(i=fe("div",{className:this._config.elements?.container?.container?.className,attributes:this._config.elements?.container?.container?.attributes}),this._coreElement.appendChild(i));let s=this.selectElement(this._config.elements?.container?.footer?.querySelector);null===s&&(s=fe("div",{className:this._config.elements?.container?.footer?.className,attributes:this._config.elements?.container?.footer?.attributes}),this._coreElement.appendChild(s)),this._eventDispatcher=new ge,this._client=new _e(this._config,this._eventDispatcher),this._components.table=new ve(i,this._config,this._eventDispatcher,this._client),this._config.components?.search?.active&&(this._components.search=new ze(n,this._config,this._eventDispatcher,this._client)),this._config.components?.filter?.active&&(this._components.filter=new Ne(n,this._config,this._eventDispatcher,this._client)),this._config.components?.pagination?.active&&(this._client.pagination={page:1,pageSize:this._config.components.pagination.pageSize},this._components.pagination=new ye(s,this._config,this._eventDispatcher,this._client))}on(e,t){this._eventDispatcher.register(e,t,1e3)}get config(){return this._config}get eventDispatcher(){return this._eventDispatcher}get client(){return this._client}refreshData(){this._client.refresh()}validateConfig(e){const t=pe.safeParse(e);if(t.success)return t.data;throw new Error(`Exception while config validation: ${t.error.message}`)}selectElement(e){return null!=e&&""!==e?document.querySelector(e):null}}});
