@hitgrab/finder 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`react/jsx-runtime`),n=require(`lodash`);function r(e){return e}function i(e){return e}function a(e){return e}function o(e){return e}function s(e){return e}function c(e,t){return{rules:e,onChange:t}}function l(e,t){return{haystack:e,onChange:t}}function u(e){let t={...e};return delete t.boolean,delete t.multiple,t}function d(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function f(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const p=(0,e.createContext)(null);function m(){let t=(0,e.useContext)(p);if(t===null)throw Error(`useFinder requires a FinderContext.`);let[n]=t;return n}function h({children:e}){let n=m();return n.state===`loading`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function g({children:e}){let n=m();return n.state===`empty`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function _({children:e}){let n=m();return n.state===`noMatches`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function v({children:e}){let n=m();return n.state===`items`&&n.matches.items?typeof e==`function`?(0,t.jsx)(e,{items:n.matches.items,pagination:n.pagination,context:n.context}):e:null}function y({children:e}){let n=m();return n.state===`groups`&&n.matches.groups?typeof e==`function`?(0,t.jsx)(e,{groups:n.matches.groups,pagination:n.pagination,context:n.context}):e:null}function b({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&(0,t.jsx)(h,{children:e.loading},`loading`),e.empty&&(0,t.jsx)(g,{children:e.empty},`empty`),e.noMatches&&(0,t.jsx)(_,{children:e.noMatches},`noMatches`),e.items&&(0,t.jsx)(v,{children:e.items},`items`),e.groups&&(0,t.jsx)(y,{children:e.groups},`groups`)]}b.Loading=h,b.Empty=g,b.NoMatches=_,b.Items=v,b.Groups=y;function x(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function S(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=C(e,x(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=w(e,x(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function C(e,t){if(e.length<t.length)return;let r=e.indexOf(t);if(r!==-1)return(0,n.range)(r,r+t.length).map(e=>e)}function w(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var T=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function E(e,t){return(Array.isArray(e)?e:[e]).map(e=>new T(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=S(n.transformed,t);return r===void 0?e:k(O(n,D(r,n.transformed)))},void 0)}function D(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function O(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function k(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function ee(e,t){return(Array.isArray(e)?e:[e]).some(e=>S(x(e),t)!==void 0)}function A({needle:n,haystack:r,Match:i=`mark`,Miss:a}){let o=(0,e.useMemo)(()=>E(r,n),[r,n]);return o===void 0?r:o.map((e,n)=>{let r=[e.value,n].join();return e.is_match?typeof i==`string`?(0,t.jsx)(i,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(i,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r):a===void 0?e.value:typeof a==`string`?(0,t.jsx)(a,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(a,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r)})}function j({Match:e=`mark`,Miss:n,children:r}){let i=m();return i.search.hasSearchTerm===!1?r:(0,t.jsx)(A,{needle:i.search.searchTerm,haystack:r,Match:e,Miss:n})}function te(){let e=new Map;return(t,r)=>t.debounceMilliseconds===void 0?r():(e.has(t)===!1&&e.set(t,(0,n.debounce)(e=>e(),t.debounceMilliseconds)),e.get(t)?.(r))}var M=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const N={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},P={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},F={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var I=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function L(e){return typeof e==`object`&&!!e&&`sortFn`in e}function R(e){return typeof e==`object`&&!!e&&`searchFn`in e}function z(e){return typeof e==`object`&&!!e&&`filterFn`in e}function B(e){return z(e)&&typeof e.options!=`function`}function V(e){return typeof e==`object`&&!!e&&`groupFn`in e}function ne(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function re(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function ie(e){return z(e)&&e.boolean===!0}function ae(e){return z(e)&&e.multiple===!0}function oe(e){return z(e)&&e.multiple!==!0&&e.boolean!==!0}function H(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function U(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new I(F.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new I(F.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new I(F.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new I(F.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function W(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function G(e){if(ae(e))return U(e);if(ie(e))return H(e);if(oe(e))return W(e);throw new I(F.INVALID_RULE_SHAPE,{definition:e})}var K=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&G(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:P.FILTERS,event:N.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(B)}get activeRules(){return this.rules.filter(e=>G(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return G(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return G(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!B(t))throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,G(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,G(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new I(F.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=G(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:P.FILTERS,event:N.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return G(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=(0,n.uniqBy)([...this.rules,...e.rules],`id`),r={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:r}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new I(F.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>B(t)&&G(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>B(r)&&G(r).isMatch(t,e.values[r.id],n)))}},q=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(V(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(V)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,r){let i=t.reduce((t,n)=>{let i=e.rule?.groupFn(n,r);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(n)}),t},{}),a=Object.entries(i).map(([e,t])=>({id:e,items:t})),o=e.rule?.sticky!==void 0,s=[],c=[];if(o&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(a,r):e.rule.sticky;t&&(s.push(se(t)),c.push(`asc`))}return e.rule?.sortGroupFn&&(s.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,r)),c.push(e.groupBySortDirection??`asc`)),s.length>0?(0,n.orderBy)(a,s,c):a}};function se({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var J=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:P.PAGINATION,event:N.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:P.PAGINATION,event:N.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?(0,n.clamp)(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?((0,n.clamp)(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let r=Math.ceil(t.length/e.numItemsPerPage),i=((0,n.clamp)(e.page,1,r)-1)*e.numItemsPerPage;return t.slice(i,i+e.numItemsPerPage)}};function ce(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function le(e,t,r){let i=t.reduce((t,i)=>{if(e.rule?.searchFn===void 0)return t;let a=e.rule.searchFn(i,r),o=(Array.isArray(a)?a.map(x):[x(a)]).reduce((t,n)=>{let r=S(n,e.searchTerm);return r!==void 0&&t.push(ce(r,n)),t},[]);if(o.length>0){let e=(0,n.orderBy)(o,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:i,score:e})}return t},[]),a=i.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return(0,n.orderBy)(i,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/a*100],[`desc`]).map(e=>e.item)}var Y=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(R)===void 0)throw new I(F.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(R)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}setSearchTerm(e){let t=this.rule;if(!t)throw new I(F.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new I(F.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:P.SEARCH,event:N.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:P.SEARCH,event:N.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new I(F.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:le(e,t,n)}};const X=[void 0,`desc`,`asc`];var Z=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(L(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(L)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=X.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(X.length-1);this.setSortDirection(X[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,r)),e.sortDirection)}},Q=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?J.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=q.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=Y.process(e.search,r,n)),e.filters&&(r=K.process(e.filters,r,n)),e.sortBy&&(r=Z.process(e.sortBy,r,n)),r}},ue=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},de=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(z(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new I(F.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[R,z,L,V],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!R(e))throw new I(F.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new I(F.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new I(F.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},fe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{ee(n.haystack,e)&&n.onChange(t,e)})}},pe=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new de(e),this.ruleEffects=new ue(t),this.searchEffects=new fe(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){R(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},me=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new M,this.#i=new Q,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(ne)??[],C=n?.filter(re)??[];this.#a=new pe({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:te()};this.search=new Y({initialSearchTerm:r},w),this.filters=new K({initialFilters:c},w),this.sortBy=new Z({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new q({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new J({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(N.CHANGE,b),y&&this.#r.on(N.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(N.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(N.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:P.CORE,event:N.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(N.READY,{source:P.CORE,event:N.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Q.test({mixins:t,items:this.items,context:this.context})}return Q.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if((0,n.isEqual)(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:P.CORE,event:N.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:P.CORE,event:N.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){(0,n.isEqual)(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;(0,n.isEqual)(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},he=class{#e;constructor(e,t){let n=()=>this;this.#e=new me(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:n,rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S,controllerRef:C,children:w}){let[T,E]=(0,e.useState)(Date.now()),[D]=(0,e.useState)(()=>{let e=new he(n,{rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S});return e.events.on(N.SYNC_UPDATED_AT,e=>E(e)),e});D.setItems(n),D.setIsLoading(f),D.setIsDisabled(m),D.setRules(r),d!==void 0&&D.setContext(d),h!==void 0&&D.pagination.setPage(h),g!==void 0&&D.pagination.setNumItemsPerPage(g),(0,e.useImperativeHandle)(C,()=>D,[D]);let O=(0,e.useMemo)(()=>[D,T],[D,T]);return(0,t.jsx)(p.Provider,{value:O,children:w})}$.Content=b,$.SearchTermHaystack=j;function ge(){return(0,e.useRef)(null)}exports.Finder=$,exports.StringMatch=A,exports.filterRule=a,exports.finderRuleset=r,exports.groupByRule=s,exports.ruleEffect=c,exports.searchEffect=l,exports.searchRule=i,exports.sortByRule=o,exports.transformFilterToBoolean=d,exports.transformFilterToMultiple=f,exports.transformFilterToSingleValue=u,exports.useFinder=m,exports.useFinderRef=ge;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`react/jsx-runtime`),n=require(`lodash`);function r(e){return e}function i(e){return e}function a(e){return e}function o(e){return e}function s(e){return e}function c(e,t){return{rules:e,onChange:t}}function l(e,t){return{haystack:e,onChange:t}}function u(e){let t={...e};return delete t.boolean,delete t.multiple,t}function d(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function f(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const p=(0,e.createContext)(null);function m(){let t=(0,e.useContext)(p);if(t===null)throw Error(`useFinder requires a FinderContext.`);let[n]=t;return n}function h({children:e}){let n=m();return n.state===`loading`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function g({children:e}){let n=m();return n.state===`empty`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function _({children:e}){let n=m();return n.state===`noMatches`?typeof e==`function`?(0,t.jsx)(e,{pagination:n.pagination,context:n.context}):e:null}function v({children:e}){let n=m();return n.state===`items`&&n.matches.items?typeof e==`function`?(0,t.jsx)(e,{items:n.matches.items,pagination:n.pagination,context:n.context}):e:null}function y({children:e}){let n=m();return n.state===`groups`&&n.matches.groups?typeof e==`function`?(0,t.jsx)(e,{groups:n.matches.groups,pagination:n.pagination,context:n.context}):e:null}function b({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&(0,t.jsx)(h,{children:e.loading},`loading`),e.empty&&(0,t.jsx)(g,{children:e.empty},`empty`),e.noMatches&&(0,t.jsx)(_,{children:e.noMatches},`noMatches`),e.items&&(0,t.jsx)(v,{children:e.items},`items`),e.groups&&(0,t.jsx)(y,{children:e.groups},`groups`)]}b.Loading=h,b.Empty=g,b.NoMatches=_,b.Items=v,b.Groups=y;function x(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function S(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=C(e,x(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=w(e,x(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function C(e,t){if(e.length<t.length)return;let r=e.indexOf(t);if(r!==-1)return(0,n.range)(r,r+t.length).map(e=>e)}function w(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var T=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function E(e,t){return(Array.isArray(e)?e:[e]).map(e=>new T(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=S(n.transformed,t);return r===void 0?e:k(O(n,D(r,n.transformed)))},void 0)}function D(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function O(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function k(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function ee(e,t){return(Array.isArray(e)?e:[e]).some(e=>S(x(e),t)!==void 0)}function A({needle:n,haystack:r,Match:i=`mark`,Miss:a}){let o=(0,e.useMemo)(()=>E(r,n),[r,n]);return o===void 0?r:o.map((e,n)=>{let r=[e.value,n].join();return e.is_match?typeof i==`string`?(0,t.jsx)(i,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(i,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r):a===void 0?e.value:typeof a==`string`?(0,t.jsx)(a,{"data-is-match":e.is_match,children:e.value},r):(0,t.jsx)(a,{"data-is-match":e.is_match,segment:e,segmentIndex:n},r)})}function j({Match:e=`mark`,Miss:n,children:r}){let i=m();return i.search.hasSearchTerm===!1?r:(0,t.jsx)(A,{needle:i.search.searchTerm,haystack:r,Match:e,Miss:n})}function te(){let e=new Map;return(t,r)=>t.debounceMilliseconds===void 0?r():(e.has(t)===!1&&e.set(t,(0,n.debounce)(e=>e(),t.debounceMilliseconds)),e.get(t)?.(r))}var M=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const N={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},P={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},F={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var I=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function L(e){return typeof e==`object`&&!!e&&`sortFn`in e}function R(e){return typeof e==`object`&&!!e&&`searchFn`in e}function z(e){return typeof e==`object`&&!!e&&`filterFn`in e}function B(e){return z(e)&&typeof e.options!=`function`}function V(e){return typeof e==`object`&&!!e&&`groupFn`in e}function ne(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function re(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function ie(e){return z(e)&&e.boolean===!0}function ae(e){return z(e)&&e.multiple===!0}function oe(e){return z(e)&&e.multiple!==!0&&e.boolean!==!0}function H(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new I(F.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function se(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new I(F.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new I(F.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new I(F.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new I(F.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new I(F.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function U(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new I(F.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new I(F.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new I(F.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function W(e){if(ae(e))return se(e);if(ie(e))return H(e);if(oe(e))return U(e);throw new I(F.INVALID_RULE_SHAPE,{definition:e})}var G=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&W(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:P.FILTERS,event:N.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(B)}get activeRules(){return this.rules.filter(e=>W(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return W(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!B(t))throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new I(F.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=W(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:P.FILTERS,event:N.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=(0,n.uniqBy)([...this.rules,...e.rules],`id`),r={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:r}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new I(F.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>B(t)&&W(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>B(r)&&W(r).isMatch(t,e.values[r.id],n)))}},K=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(V(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(V)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:P.GROUP_BY,event:N.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,r){let i=t.reduce((t,n)=>{let i=e.rule?.groupFn(n,r);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(n)}),t},{}),a=Object.entries(i).map(([e,t])=>({id:e,items:t})),o=e.rule?.sticky!==void 0,s=[],c=[];if(o&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(a,r):e.rule.sticky;t&&(s.push(ce(t)),c.push(`asc`))}return e.rule?.sortGroupFn&&(s.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,r)),c.push(e.groupBySortDirection??`asc`)),s.length>0?(0,n.orderBy)(a,s,c):a}};function ce({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var q=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:P.PAGINATION,event:N.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:P.PAGINATION,event:N.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?(0,n.clamp)(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?((0,n.clamp)(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let r=Math.ceil(t.length/e.numItemsPerPage),i=((0,n.clamp)(e.page,1,r)-1)*e.numItemsPerPage;return t.slice(i,i+e.numItemsPerPage)}};function le(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function J(e,t,r){let i=t.reduce((t,i)=>{if(e.rule?.searchFn===void 0)return t;let a=e.rule.searchFn(i,r),o=(Array.isArray(a)?a.map(x):[x(a)]).reduce((t,n)=>{let r=S(n,e.searchTerm);return r!==void 0&&t.push(le(r,n)),t},[]);if(o.length>0){let e=(0,n.orderBy)(o,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:i,score:e})}return t},[]),a=i.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return(0,n.orderBy)(i,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/a*100],[`desc`]).map(e=>e.item)}var Y=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(R)===void 0)throw new I(F.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(R)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}get suggestedFilters(){let e=[];if(this.hasSearchTerm&&this.rule?.suggestFilters){let t=this.#e.getRuleBook().rules.filter(B),n=t.filter(e=>e.options&&e.options.length>0);n.length>0&&n.forEach(t=>{let n=J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},t.options??[]);n.length>0&&e.push({rule:t,optionMatches:n})});let r=t.filter(e=>e.label!==void 0).filter(t=>e.some(e=>e.rule===t)===!1);r.length>0&&J({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},r).forEach(t=>{e.push({rule:t})})}return e}setSearchTerm(e){let t=this.rule;if(!t)throw new I(F.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new I(F.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:P.SEARCH,event:N.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:P.SEARCH,event:N.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,suggestedFilters:this.suggestedFilters,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new I(F.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:J(e,t,n)}};const X=[void 0,`desc`,`asc`];var Z=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(L(t)===!1)throw new I(F.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(L)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=X.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(X.length-1);this.setSortDirection(X[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:P.SORT_BY,event:N.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,r)),e.sortDirection)}},Q=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?q.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=K.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=Y.process(e.search,r,n)),e.filters&&(r=G.process(e.filters,r,n)),e.sortBy&&(r=Z.process(e.sortBy,r,n)),r}},ue=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},de=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(z(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new I(F.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[R,z,L,V],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!R(e))throw new I(F.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new I(F.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new I(F.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},fe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{ee(n.haystack,e)&&n.onChange(t,e)})}},pe=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new de(e),this.ruleEffects=new ue(t),this.searchEffects=new fe(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){R(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},me=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new M,this.#i=new Q,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(ne)??[],C=n?.filter(re)??[];this.#a=new pe({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:te()};this.search=new Y({initialSearchTerm:r},w),this.filters=new G({initialFilters:c},w),this.sortBy=new Z({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new q({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(N.CHANGE,b),y&&this.#r.on(N.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(N.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(N.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:P.CORE,event:N.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(N.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(N.READY,{source:P.CORE,event:N.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Q.test({mixins:t,items:this.items,context:this.context})}return Q.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if((0,n.isEqual)(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:P.CORE,event:N.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:P.CORE,event:N.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){(0,n.isEqual)(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;(0,n.isEqual)(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:P.CORE,event:N.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},he=class{#e;constructor(e,t){let n=()=>this;this.#e=new me(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:n,rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S,controllerRef:C,children:w}){let[T,E]=(0,e.useState)(Date.now()),[D]=(0,e.useState)(()=>{let e=new he(n,{rules:r,effects:i,initialSearchTerm:a,initialSortBy:o,initialSortDirection:s,initialGroupBy:c,initialGroupBySortDirection:l,initialFilters:u,context:d,isLoading:f,disabled:m,page:h,numItemsPerPage:g,requireGroup:_,ignoreSortByRulesWhileSearchRuleIsActive:v,onInit:y,onReady:b,onFirstUserInteraction:x,onChange:S});return e.events.on(N.SYNC_UPDATED_AT,e=>E(e)),e});D.setItems(n),D.setIsLoading(f),D.setIsDisabled(m),D.setRules(r),d!==void 0&&D.setContext(d),h!==void 0&&D.pagination.setPage(h),g!==void 0&&D.pagination.setNumItemsPerPage(g),(0,e.useImperativeHandle)(C,()=>D,[D]);let O=(0,e.useMemo)(()=>[D,T],[D,T]);return(0,t.jsx)(p.Provider,{value:O,children:w})}$.Content=b,$.SearchTermHaystack=j;function ge(){return(0,e.useRef)(null)}exports.Finder=$,exports.StringMatch=A,exports.filterRule=a,exports.finderRuleset=r,exports.groupByRule=s,exports.ruleEffect=c,exports.searchEffect=l,exports.searchRule=i,exports.sortByRule=o,exports.transformFilterToBoolean=d,exports.transformFilterToMultiple=f,exports.transformFilterToSingleValue=u,exports.useFinder=m,exports.useFinderRef=ge;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import { FilterOption as FilterOption$1 } from "@hitgrab/finder";
1
2
  import { ElementType, PropsWithChildren, ReactElement, ReactNode, RefObject } from "react";
2
3
 
3
4
  //#region src/core/types/rule-types.d.ts
@@ -15,6 +16,7 @@ type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, F
15
16
  interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
16
17
  id?: string;
17
18
  searchFn?: (item: FItem, context: FContext) => string | string[];
19
+ suggestFilters?: boolean;
18
20
  }
19
21
  /**
20
22
  * Describes the display of a filter or sort option.
@@ -189,6 +191,7 @@ declare class FinderCore<FItem = any, FContext = any> {
189
191
  get search(): {
190
192
  rule: SearchRuleDefinition<unknown, any> | undefined;
191
193
  searchTerm: string;
194
+ suggestedFilters: SearchRuleSuggestion[];
192
195
  hasSearchTerm: boolean;
193
196
  hasSearchRule: boolean;
194
197
  setSearchTerm: (value: string) => void;
@@ -349,6 +352,10 @@ interface PaginationMixinInterface {
349
352
  setPage: (value: number) => void;
350
353
  setNumItemsPerPage: (value: number) => void;
351
354
  }
355
+ interface SearchRuleSuggestion {
356
+ rule: RuleDefinition;
357
+ optionMatches?: FilterOption$1[];
358
+ }
352
359
  //#endregion
353
360
  //#region src/core/types/string-match-types.d.ts
354
361
  /**
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ElementType, PropsWithChildren, ReactElement, ReactNode, RefObject } from "react";
2
+ import { FilterOption as FilterOption$1 } from "@hitgrab/finder";
2
3
 
3
4
  //#region src/core/types/rule-types.d.ts
4
5
  /**
@@ -15,6 +16,7 @@ type RuleDefinition<FItem = any, FContext = any> = SearchRuleDefinition<FItem, F
15
16
  interface SearchRuleDefinition<FItem = any, FContext = any> extends Omit<Rule, "id"> {
16
17
  id?: string;
17
18
  searchFn?: (item: FItem, context: FContext) => string | string[];
19
+ suggestFilters?: boolean;
18
20
  }
19
21
  /**
20
22
  * Describes the display of a filter or sort option.
@@ -189,6 +191,7 @@ declare class FinderCore<FItem = any, FContext = any> {
189
191
  get search(): {
190
192
  rule: SearchRuleDefinition<unknown, any> | undefined;
191
193
  searchTerm: string;
194
+ suggestedFilters: SearchRuleSuggestion[];
192
195
  hasSearchTerm: boolean;
193
196
  hasSearchRule: boolean;
194
197
  setSearchTerm: (value: string) => void;
@@ -349,6 +352,10 @@ interface PaginationMixinInterface {
349
352
  setPage: (value: number) => void;
350
353
  setNumItemsPerPage: (value: number) => void;
351
354
  }
355
+ interface SearchRuleSuggestion {
356
+ rule: RuleDefinition;
357
+ optionMatches?: FilterOption$1[];
358
+ }
352
359
  //#endregion
353
360
  //#region src/core/types/string-match-types.d.ts
354
361
  /**
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createContext as e,useContext as t,useImperativeHandle as n,useMemo as r,useRef as i,useState as a}from"react";import{jsx as o}from"react/jsx-runtime";import{clamp as s,debounce as c,isEqual as l,orderBy as u,range as d,uniqBy as f}from"lodash";function p(e){return e}function m(e){return e}function h(e){return e}function g(e){return e}function _(e){return e}function v(e,t){return{rules:e,onChange:t}}function y(e,t){return{haystack:e,onChange:t}}function b(e){let t={...e};return delete t.boolean,delete t.multiple,t}function x(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function S(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const C=e(null);function w(){let e=t(C);if(e===null)throw Error(`useFinder requires a FinderContext.`);let[n]=e;return n}function T({children:e}){let t=w();return t.state===`loading`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function E({children:e}){let t=w();return t.state===`empty`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function D({children:e}){let t=w();return t.state===`noMatches`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function O({children:e}){let t=w();return t.state===`items`&&t.matches.items?typeof e==`function`?o(e,{items:t.matches.items,pagination:t.pagination,context:t.context}):e:null}function k({children:e}){let t=w();return t.state===`groups`&&t.matches.groups?typeof e==`function`?o(e,{groups:t.matches.groups,pagination:t.pagination,context:t.context}):e:null}function A({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&o(T,{children:e.loading},`loading`),e.empty&&o(E,{children:e.empty},`empty`),e.noMatches&&o(D,{children:e.noMatches},`noMatches`),e.items&&o(O,{children:e.items},`items`),e.groups&&o(k,{children:e.groups},`groups`)]}A.Loading=T,A.Empty=E,A.NoMatches=D,A.Items=O,A.Groups=k;function j(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function M(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=ee(e,j(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=te(e,j(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function ee(e,t){if(e.length<t.length)return;let n=e.indexOf(t);if(n!==-1)return d(n,n+t.length).map(e=>e)}function te(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var ne=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function re(e,t){return(Array.isArray(e)?e:[e]).map(e=>new ne(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=M(n.transformed,t);return r===void 0?e:oe(ae(n,ie(r,n.transformed)))},void 0)}function ie(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function ae(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function oe(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function se(e,t){return(Array.isArray(e)?e:[e]).some(e=>M(j(e),t)!==void 0)}function N({needle:e,haystack:t,Match:n=`mark`,Miss:i}){let a=r(()=>re(t,e),[t,e]);return a===void 0?t:a.map((e,t)=>{let r=[e.value,t].join();return e.is_match?typeof n==`string`?o(n,{"data-is-match":e.is_match,children:e.value},r):o(n,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r):i===void 0?e.value:typeof i==`string`?o(i,{"data-is-match":e.is_match,children:e.value},r):o(i,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r)})}function P({Match:e=`mark`,Miss:t,children:n}){let r=w();return r.search.hasSearchTerm===!1?n:o(N,{needle:r.search.searchTerm,haystack:n,Match:e,Miss:t})}function ce(){let e=new Map;return(t,n)=>t.debounceMilliseconds===void 0?n():(e.has(t)===!1&&e.set(t,c(e=>e(),t.debounceMilliseconds)),e.get(t)?.(n))}var le=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const F={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},I={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},L={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var R=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function z(e){return typeof e==`object`&&!!e&&`sortFn`in e}function B(e){return typeof e==`object`&&!!e&&`searchFn`in e}function V(e){return typeof e==`object`&&!!e&&`filterFn`in e}function H(e){return V(e)&&typeof e.options!=`function`}function U(e){return typeof e==`object`&&!!e&&`groupFn`in e}function ue(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function de(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function fe(e){return V(e)&&e.boolean===!0}function pe(e){return V(e)&&e.multiple===!0}function me(e){return V(e)&&e.multiple!==!0&&e.boolean!==!0}function he(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new R(L.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new R(L.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new R(L.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new R(L.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function ge(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new R(L.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new R(L.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new R(L.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new R(L.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new R(L.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new R(L.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function _e(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new R(L.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new R(L.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new R(L.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function W(e){if(pe(e))return ge(e);if(fe(e))return he(e);if(me(e))return _e(e);throw new R(L.INVALID_RULE_SHAPE,{definition:e})}var G=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&W(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:I.FILTERS,event:F.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(H)}get activeRules(){return this.rules.filter(e=>W(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return W(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!H(t))throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,W(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new R(L.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=W(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:I.FILTERS,event:F.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return W(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=f([...this.rules,...e.rules],`id`),n={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:n}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new R(L.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>H(t)&&W(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>H(r)&&W(r).isMatch(t,e.values[r.id],n)))}},K=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(U(t)===!1)throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(U)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:I.GROUP_BY,event:F.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:I.GROUP_BY,event:F.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,n){let r=t.reduce((t,r)=>{let i=e.rule?.groupFn(r,n);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(r)}),t},{}),i=Object.entries(r).map(([e,t])=>({id:e,items:t})),a=e.rule?.sticky!==void 0,o=[],s=[];if(a&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(i,n):e.rule.sticky;t&&(o.push(ve(t)),s.push(`asc`))}return e.rule?.sortGroupFn&&(o.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,n)),s.push(e.groupBySortDirection??`asc`)),o.length>0?u(i,o,s):i}};function ve({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var q=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:I.PAGINATION,event:F.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:I.PAGINATION,event:F.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?s(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?(s(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let n=Math.ceil(t.length/e.numItemsPerPage),r=(s(e.page,1,n)-1)*e.numItemsPerPage;return t.slice(r,r+e.numItemsPerPage)}};function ye(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function be(e,t,n){let r=t.reduce((t,r)=>{if(e.rule?.searchFn===void 0)return t;let i=e.rule.searchFn(r,n),a=(Array.isArray(i)?i.map(j):[j(i)]).reduce((t,n)=>{let r=M(n,e.searchTerm);return r!==void 0&&t.push(ye(r,n)),t},[]);if(a.length>0){let e=u(a,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:r,score:e})}return t},[]),i=r.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return u(r,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/i*100],[`desc`]).map(e=>e.item)}var J=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(B)===void 0)throw new R(L.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(B)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}setSearchTerm(e){let t=this.rule;if(!t)throw new R(L.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new R(L.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:I.SEARCH,event:F.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:I.SEARCH,event:F.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new R(L.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:be(e,t,n)}};const Y=[void 0,`desc`,`asc`];var X=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(z(t)===!1)throw new R(L.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(z)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:I.SORT_BY,event:F.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=Y.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(Y.length-1);this.setSortDirection(Y[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:I.SORT_BY,event:F.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,n){return e.rule===void 0?t:u(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,n)),e.sortDirection)}},Z=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?q.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=K.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=J.process(e.search,r,n)),e.filters&&(r=G.process(e.filters,r,n)),e.sortBy&&(r=X.process(e.sortBy,r,n)),r}},xe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},Se=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(V(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new R(L.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[B,V,z,U],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!B(e))throw new R(L.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new R(L.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new R(L.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},Ce=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{se(n.haystack,e)&&n.onChange(t,e)})}},we=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new Se(e),this.ruleEffects=new xe(t),this.searchEffects=new Ce(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){B(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},Q=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new le,this.#i=new Z,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(ue)??[],C=n?.filter(de)??[];this.#a=new we({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:ce()};this.search=new J({initialSearchTerm:r},w),this.filters=new G({initialFilters:c},w),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new q({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:I.CORE,event:F.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(F.CHANGE,b),y&&this.#r.on(F.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:I.CORE,event:F.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(F.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(F.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(F.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:I.CORE,event:F.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(F.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(F.READY,{source:I.CORE,event:F.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Z.test({mixins:t,items:this.items,context:this.context})}return Z.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if(l(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:I.CORE,event:F.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:I.CORE,event:F.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:I.CORE,event:F.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){l(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;l(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:I.CORE,event:F.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},Te=class{#e;constructor(e,t){let n=()=>this;this.#e=new Q(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:e,rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w,controllerRef:T,children:E}){let[D,O]=a(Date.now()),[k]=a(()=>{let n=new Te(e,{rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w});return n.events.on(F.SYNC_UPDATED_AT,e=>O(e)),n});k.setItems(e),k.setIsLoading(m),k.setIsDisabled(h),k.setRules(t),p!==void 0&&k.setContext(p),g!==void 0&&k.pagination.setPage(g),_!==void 0&&k.pagination.setNumItemsPerPage(_),n(T,()=>k,[k]);let A=r(()=>[k,D],[k,D]);return o(C.Provider,{value:A,children:E})}$.Content=A,$.SearchTermHaystack=P;function Ee(){return i(null)}export{$ as Finder,N as StringMatch,h as filterRule,p as finderRuleset,_ as groupByRule,v as ruleEffect,y as searchEffect,m as searchRule,g as sortByRule,x as transformFilterToBoolean,S as transformFilterToMultiple,b as transformFilterToSingleValue,w as useFinder,Ee as useFinderRef};
1
+ import{createContext as e,useContext as t,useImperativeHandle as n,useMemo as r,useRef as i,useState as a}from"react";import{jsx as o}from"react/jsx-runtime";import{clamp as s,debounce as c,isEqual as l,orderBy as u,range as d,uniqBy as f}from"lodash";function p(e){return e}function m(e){return e}function h(e){return e}function g(e){return e}function _(e){return e}function v(e,t){return{rules:e,onChange:t}}function y(e,t){return{haystack:e,onChange:t}}function b(e){let t={...e};return delete t.boolean,delete t.multiple,t}function x(e){let t={...e};return delete t.multiple,{...t,boolean:!0}}function S(e){let t={...e};return delete t.boolean,{...t,multiple:!0}}const C=e(null);function w(){let e=t(C);if(e===null)throw Error(`useFinder requires a FinderContext.`);let[n]=e;return n}function T({children:e}){let t=w();return t.state===`loading`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function E({children:e}){let t=w();return t.state===`empty`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function D({children:e}){let t=w();return t.state===`noMatches`?typeof e==`function`?o(e,{pagination:t.pagination,context:t.context}):e:null}function O({children:e}){let t=w();return t.state===`items`&&t.matches.items?typeof e==`function`?o(e,{items:t.matches.items,pagination:t.pagination,context:t.context}):e:null}function k({children:e}){let t=w();return t.state===`groups`&&t.matches.groups?typeof e==`function`?o(e,{groups:t.matches.groups,pagination:t.pagination,context:t.context}):e:null}function A({children:e}){if(Object.values(e).length===0)throw Error(`No render props were found.`);return[e.loading&&o(T,{children:e.loading},`loading`),e.empty&&o(E,{children:e.empty},`empty`),e.noMatches&&o(D,{children:e.noMatches},`noMatches`),e.items&&o(O,{children:e.items},`items`),e.groups&&o(k,{children:e.groups},`groups`)]}A.Loading=T,A.Empty=E,A.NoMatches=D,A.Items=O,A.Groups=k;function j(e){return e.toLowerCase().replace(/[^\w\d]+/g,``)}function M(e,t){let n=new RegExp(/"(.*?)"/g),r=t,i=[],a,o=!1;for(;(a=n.exec(t))!==null&&o===!1;){let t=ee(e,j(String(a[1])));t===void 0?o=!0:(i=i.concat(t),r=r.replace(a[0],``))}if(o)return;let s=te(e,j(r));if(s!==void 0)return i=i.concat(s),i.sort((e,t)=>e-t)}function ee(e,t){if(e.length<t.length)return;let n=e.indexOf(t);if(n!==-1)return d(n,n+t.length).map(e=>e)}function te(e,t){if(e.length<t.length)return;let n=Array.from(t),r=[],i=e,a=0,o=!1;for(let e of n){let t=i.indexOf(e);if(t===-1&&(o=!0),o===!1){r.push(a+t);let e=t+1;a+=e,i=i.substring(e)}}if(!o)return r}var ne=class e{#e;constructor(t){this.source=t;let n=e.composeTransformedHaystackSegments(t);this.#e=n,this.transformed=n.map(e=>e.value).join(``).toLowerCase()}getSourceCharacterIndex(e){let t=0;return this.#e.reduce((n,r)=>{if(n!==1/0)return n;let i=t+r.value.length;if(e>=t&&e<=i){let i=e-t;n=r.index+i}return t+=r.value.length,n},1/0)}static composeTransformedHaystackSegments(e){let t=e.matchAll(/[\w\d]+/g),n=[];for(let e of t)n.push({value:e[0],index:e.index,length:e[0].length});return n}};function re(e,t){return(Array.isArray(e)?e:[e]).map(e=>new ne(e)).reduce((e,n)=>{if(e!==void 0)return e;let r=M(n.transformed,t);return r===void 0?e:oe(ae(n,ie(r,n.transformed)))},void 0)}function ie(e,t){let n=[...e],r=[],i=0;for(;n.length>0&&i<100;){let e=n.at(0);if(e===void 0)throw Error(`Should never get here`);let a=1;for(let t=1;t<=n.length;t+=1){let r=n.at(t);r!==void 0&&e+t===r&&(a+=1)}let o=e,s=e+a,c=t.substring(o,s);r.push({index:o,value:c,is_match:!0,length:c.length}),n.splice(0,a),i+=1}return r}function ae(e,t){return t.reduce((n,r,i)=>{if(i===0&&r.index!==0){let t=e.source.substring(0,e.getSourceCharacterIndex(r.index));n.push({index:0,value:t,is_match:!1,length:t.length})}let a=e.getSourceCharacterIndex(r.index),o=e.getSourceCharacterIndex(r.index+r.length),s=e.source.substring(a,o);n.push({index:a,value:s,is_match:!0,length:s.length});let c=t.at(i+1);if(c){let t=e.getSourceCharacterIndex(r.index+r.length),i=e.getSourceCharacterIndex(c.index),a=e.source.substring(t,i);n.push({index:t,value:a,is_match:!1,length:a.length})}else if(a+s.length!==e.source.length){let t=a+s.length,r=e.source.substring(t);n.push({index:t,value:r,is_match:!1,length:r.length})}return n},[])}function oe(e){let t=/\S/,n=[...e];return n.forEach((e,r)=>{if(e.is_match){let i=e.value.search(t);if(i!==0){let t=n.at(r-1);t&&(t.length+=i,t.value+=e.value.substring(0,i),e.value=e.value.substring(i),e.index+=i)}}}),n}function se(e,t){return(Array.isArray(e)?e:[e]).some(e=>M(j(e),t)!==void 0)}function N({needle:e,haystack:t,Match:n=`mark`,Miss:i}){let a=r(()=>re(t,e),[t,e]);return a===void 0?t:a.map((e,t)=>{let r=[e.value,t].join();return e.is_match?typeof n==`string`?o(n,{"data-is-match":e.is_match,children:e.value},r):o(n,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r):i===void 0?e.value:typeof i==`string`?o(i,{"data-is-match":e.is_match,children:e.value},r):o(i,{"data-is-match":e.is_match,segment:e,segmentIndex:t},r)})}function ce({Match:e=`mark`,Miss:t,children:n}){let r=w();return r.search.hasSearchTerm===!1?n:o(N,{needle:r.search.searchTerm,haystack:n,Match:e,Miss:t})}function le(){let e=new Map;return(t,n)=>t.debounceMilliseconds===void 0?n():(e.has(t)===!1&&e.set(t,c(e=>e(),t.debounceMilliseconds)),e.get(t)?.(n))}var ue=class{#e=new Map;#t=!1;on(e,t){this.#e.has(e)===!1&&this.#e.set(e,new Set),this.#e.get(e)?.add(t)}off(e,t){if(this.#e.has(e)!==!1){if(t===void 0){this.#e.delete(e);return}this.#e.get(e)?.delete(t)}}emit(e,t){this.#t||this.#e.get(e)?.forEach(e=>{e(t)})}silently(e){this.#t=!0,e(),this.#t=!1}isSilent(){return this.#t}};const P={INIT:`init`,FIRST_USER_INTERACTION:`firstUserInteraction`,READY:`ready`,CHANGE:`change`,SET_ITEMS:`setItems`,SET_IS_LOADING:`setIsLoading`,SET_IS_DISABLED:`setIsDisabled`,SET_CONTEXT:`setContext`,SET_SEARCH_TERM:`setSearchTerm`,RESET_SEARCH_TERM:`resetSearchTerm`,SET_FILTER:`setFilter`,RESET_FILTERS:`resetFilters`,SET_SORT_BY:`setSortBy`,SET_SORT_BY_DIRECTION:`setSortDirection`,SET_GROUP_BY:`setGroupBy`,SET_GROUP_SORT_BY_DIRECTION:`setGroupBySortDirection`,SET_PAGE:`setPage`,SET_NUM_ITEMS_PER_PAGE:`setNumItemsPerPage`,SYNC_UPDATED_AT:`syncUpdatedAt`},F={CORE:`core`,SEARCH:`search`,FILTERS:`filters`,GROUP_BY:`groupBy`,SORT_BY:`sortBy`,PAGINATION:`pagination`},I={RULE_NOT_FOUND:`Finder could not locate requested rule`,WRONG_RULE_TYPE_FOR_MIXIN:`The requested rule is not valid for this mixin.`,NO_SEARCH_RULE_SET:`Unable to set search term; no SearchRule was found.`,INVALID_SEARCH_TERM_TYPE:`Search terms must be strings.`,NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS:`Finder could not find a matching option with the passed value could be found. If this is intended, set 'strictOptions:false' in the rule definition.`,SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY:`Finder could not set this filter value, as the rule requires an array.`,ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE:`Finder could not add to this filter, as no optionValue was passed.`,DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER:`Finder could not delete an option from this filter, as it does not support multiple values.`,SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE:`Finder could not set this filter value, as the rule requires a boolean.`,ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER:`Finder could not add to this filter value, as the rule is a boolean.`,TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS:`Finder could not toggle this filter rule option, as the filter does not have any options.`,TOGGLING_OPTION_THAT_DOES_NOT_EXIST:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_OPTION_WITHOUT_PASSING_OPTION:`Finder could not toggle this filter rule option, as the option was not found.`,TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE:`Finder could not toggle this filter rule option, as boolean filters do not have options.`,TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS:`Finder was unable to test the options for this filter rule. It must be a boolean or have defined options.`,INVALID_RULE_WITHOUT_ID:`Init failed: Missing rule id.`,INVALID_RULE_SHAPE:`Init failed: Malformed rule definition`,INVALID_RULE_DUPLICATE:`Init failed: Duplicate rule id.`};var L=class extends Error{constructor(e,t){let n=`${e} ${JSON.stringify({...t})}`;super(n),this.name=`FinderError`}};function R(e){return typeof e==`object`&&!!e&&`sortFn`in e}function z(e){return typeof e==`object`&&!!e&&`searchFn`in e}function B(e){return typeof e==`object`&&!!e&&`filterFn`in e}function V(e){return B(e)&&typeof e.options!=`function`}function H(e){return typeof e==`object`&&!!e&&`groupFn`in e}function de(e){return typeof e==`object`&&!!e&&(`rules`in e||`callback`in e)}function fe(e){return typeof e==`object`&&!!e&&(`haystack`in e||`callback`in e)}function pe(e){return B(e)&&e.boolean===!0}function me(e){return B(e)&&e.multiple===!0}function he(e){return B(e)&&e.multiple!==!0&&e.boolean!==!0}function ge(e){return{validate(t){if(t!==void 0&&typeof t!=`boolean`)throw new L(I.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:t});return!0},parse(t){return typeof t!=`boolean`&&t!==void 0?!1:t===void 0?e.required?!0:e.defaultValue===void 0?!1:e.defaultValue:t},has(e){return this.parse(e)},toggle(t,n){let r=this.parse(t);if(typeof r!=`boolean`)throw new L(I.SETTING_BOOLEAN_FILTER_WITHOUT_BOOLEAN_VALUE,{rule:e,value:r,optionValue:n});return!r},add(t,n){throw new L(I.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new L(I.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:this.parse(t)===!0},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function _e(e){return{validate(t){if(t!==void 0&&Array.isArray(t)===!1)throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t});if(e.strictOptions&&Array.isArray(e.options)&&Array.isArray(t)&&t.every(t=>e.options?.some(e=>e.value===t))===!1)throw new L(I.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0)return e.required&&Array.isArray(e.options)&&e.options.length>0?[e.options.at(0)?.value]:[];if(Array.isArray(t))return t;throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:t})},has(t,n){if(n===void 0)return Array.isArray(t)&&t.length>0;if(Array.isArray(n))return n.every(t=>this.has(e,t));let r=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return Array.isArray(t)&&r!==void 0&&t.includes(r.value)},toggle(t,n){let r=this.parse(t);if(Array.isArray(r)===!1)throw new L(I.SETTING_MULTIPLE_FILTER_WITHOUT_ARRAY,{rule:e,value:r});if(n===void 0)throw new L(I.TOGGLING_OPTION_WITHOUT_PASSING_OPTION,{rule:e});if(e.options===void 0)throw new L(I.TOGGLING_OPTION_ON_RULE_WITH_NO_OPTIONS,{rule:e,optionValue:n});let i=e.options.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);if(i===void 0)throw new L(I.TOGGLING_OPTION_THAT_DOES_NOT_EXIST,{rule:e,optionValue:n});return r.includes(i.value)?[...r.filter(e=>e!==i.value)]:[...r,i.value]},add(t,n){let r=this.parse(t);if(n===void 0)throw new L(I.ADDING_OPTION_TO_MULTIPLE_FILTER_WITHOUT_OPTION_VALUE,{rule:e,optionValue:n});let i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i===void 0?[...r,n]:r.includes(i.value)===!1?[...r,i.value]:r},delete(t,n){if(n===void 0)return;let r=this.parse(t),i=e.options?.find(e=>typeof n==`object`&&`value`in n?e.value===n.value:e.value===n);return i!==void 0&&r.includes(i.value)?r.filter(e=>e!==i.value):r.filter(e=>e!==n)},isActive(t){return e.required?!0:this.parse(t).length>0},isMatch(t,n,r){return Array.isArray(n)?n.some(n=>e.filterFn(t,n,r)):!1}}}function ve(e){return{validate(t){if(e.strictOptions&&Array.isArray(e.options)&&e.options.find(e=>e.value===t)===void 0)throw new L(I.NO_MATCHING_OPTION_FOUND_WITH_STRICT_OPTIONS,{rule:e,value:t});return!0},parse(t){if(t===void 0&&e.required){if(e.defaultValue)return e.defaultValue;if(Array.isArray(e.options)&&e.options.length>0)return e.options.at(0)?.value}return t},has(e){return e!==void 0},toggle(e,t=void 0,n){if(!(e===t&&n===!1))return t},add(t,n){throw new L(I.ADDING_OPTION_VALUE_TO_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},delete(t,n){if(n!==void 0)throw new L(I.DELETING_OPTION_VALUE_FROM_NON_MULTIPLE_FILTER,{rule:e,value:t,optionValue:n})},isActive(t){return e.required?!0:!(t===void 0||typeof t==`string`&&t.trim()===``)},isMatch(t,n,r){return e.filterFn(t,n,r)}}}function U(e){if(me(e))return _e(e);if(pe(e))return ge(e);if(he(e))return ve(e);throw new L(I.INVALID_RULE_SHAPE,{definition:e})}var W=class{#e;#t;constructor({initialFilters:e},t){this.#e=e??{},this.#t=t}set(e,t){if(this.#t.isDisabled())return;let n=this.getRule(e),r=this.get(e),i=typeof t==`string`&&t.trim()===``||t==null,a=i?void 0:t;i===!1&&U(n).validate(a),!(this.#e[n.id]!==void 0&&this.#e[n.id]===a)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:a},this.#t.touch({source:F.FILTERS,event:P.SET_FILTER,current:a,initial:r,rule:n})})}get rules(){return this.#t.getRuleBook().rules.filter(V)}get activeRules(){return this.rules.filter(e=>U(e).isActive(this.#e[e.id]))}get(e){let t=this.getRule(e),n=this.#e[t.id];return U(t).parse(n)}has(e,t){let n=this.getRule(e),r=this.#e[n.id];return U(n).has(r,t)}getRule(e){let t=this.#t.getRuleBook().getRule(e);if(!V(t))throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}add(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,U(n).add(r,t))}delete(e,t){let n=this.getRule(e),r=this.#e[n.id];this.set(n,U(n).delete(r,t))}toggle(e,t){let n=this.getRule(e),r=this.#e[n.id];if(n.boolean&&t!==void 0)throw new L(I.TOGGLING_BOOLEAN_FILTER_WITH_UNUSED_VALUE,{rule:n,value:r});let i=U(n).toggle(r,t,!!n.required);this.set(n,i)}reset(){let e=this.values;this.#e={},this.#t.touch({source:F.FILTERS,event:P.RESET_FILTERS,current:this.values,initial:e})}isRuleActive(e){let t=this.getRule(e),n=this.#e[t.id];return U(t).isActive(n)}test(e){if(this.#t.isLoading())return[];if(e.isAdditive){let t=f([...this.rules,...e.rules],`id`),n={...this.values,...e.values};return this.#t.test({filters:{rules:t,values:n}},!0)}return this.#t.test({filters:{rules:e.rules,values:e.values??{}}})}testRule({rule:e,value:t,...n}){let r=this.getRule(e);return this.test({rules:[r],values:{[r.id]:t},...n})}testRuleOptions(e,t){if(this.#t.isLoading())return new Map;let n=this.getRule(e);if(n.boolean){let e=new Map;return e.set(!0,this.testRule({rule:n,value:!0})),e.set(!1,this.testRule({rule:n,value:!1})),e}if(Array.isArray(n.options)){let e=new Map;return n.options.forEach(r=>{let i;i=n.multiple?[r.value]:r.value,e.set(r,this.testRule({rule:n,value:i,isAdditive:t}))}),e}throw new L(I.TESTING_OPTIONS_ON_RULE_WITH_NO_OPTIONS,n)}get values(){return this.rules.reduce((e,t)=>(e[t.id]=this.get(t),e),{})}get raw(){return this.#e}serialize(){return{rules:this.rules,values:this.values}}get api(){return{values:this.values,raw:this.raw,activeRules:this.activeRules,rules:this.rules,isActive:this.isRuleActive.bind(this),get:this.get.bind(this),add:this.add.bind(this),has:this.has.bind(this),getRule:this.getRule.bind(this),toggle:this.toggle.bind(this),set:this.set.bind(this),delete:this.delete.bind(this),reset:this.reset.bind(this),test:this.test.bind(this),testRule:this.testRule.bind(this),testRuleOptions:this.testRuleOptions.bind(this)}}static process(e,t,n){let r=e.rules.filter(t=>V(t)&&U(t).isActive(e.values[t.id]));return r.length===0?t:t.filter(t=>r.every(r=>V(r)&&U(r).isMatch(t,e.values[r.id],n)))}},G=class{#e;#t;#n;constructor({initialGroupBy:e,initialGroupBySortDirection:t,requireGroup:n},r){this.#n=r,e&&(this.#e=this.getRule(e)),this.#t=t,this.requireGroup=n}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(H(t)===!1)throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(H)}get activeRule(){let e=this.requireGroup?this.rules.at(0):void 0;return this.#e??e}get hasGroupByRule(){return this.activeRule!==void 0}get groupBySortDirection(){return this.#t??this.activeRule?.defaultGroupSortDirection}set(e){if(this.#n.isDisabled())return;let t=this.#e,n,r=typeof e==`string`&&e.trim()===``;r&&(n=void 0),r===!1&&e!==void 0&&(n=this.getRule(e)),this.#e!==n&&(this.#e=n,this.#t=void 0,this.#n.touch({source:F.GROUP_BY,event:P.SET_GROUP_BY,current:n?.id,initial:t?.id,rule:n}))}setGroupSortDirection(e){let t=this.#t;this.#t=e,this.#n.touch({source:F.GROUP_BY,event:P.SET_GROUP_SORT_BY_DIRECTION,current:e,initial:t,rule:this.activeRule})}toggle(e){let t=this.getRule(e);if(this.activeRule===t){this.set(void 0);return}this.set(t)}reset(){this.setGroupSortDirection(void 0),this.set(void 0)}serialize(){return{rule:this.activeRule,groupBySortDirection:this.groupBySortDirection}}get api(){return{activeRule:this.activeRule,requireGroup:this.requireGroup,rules:this.rules,groupBySortDirection:this.groupBySortDirection,set:this.set.bind(this),toggle:this.toggle.bind(this),setGroupSortDirection:this.setGroupSortDirection.bind(this),reset:this.reset.bind(this)}}static process(e,t,n){let r=t.reduce((t,r)=>{let i=e.rule?.groupFn(r,n);return i&&(Array.isArray(i)?i:[i]).forEach(e=>{t[e]??=[],t[e].push(r)}),t},{}),i=Object.entries(r).map(([e,t])=>({id:e,items:t})),a=e.rule?.sticky!==void 0,o=[],s=[];if(a&&e.rule){let t=typeof e.rule.sticky==`function`?e.rule.sticky(i,n):e.rule.sticky;t&&(o.push(ye(t)),s.push(`asc`))}return e.rule?.sortGroupFn&&(o.push(t=>e.rule?.sortGroupFn===void 0?0:e.rule.sortGroupFn(t,n)),s.push(e.groupBySortDirection??`asc`)),o.length>0?u(i,o,s):i}};function ye({header:e,footer:t}){let n=[];e!==void 0&&(Array.isArray(e)&&(n=e),typeof e==`string`&&(n=[e]));let r=[];return t!==void 0&&(Array.isArray(t)&&(r=t),typeof t==`string`&&(r=[t])),e=>{if(n.includes(e.id)){let t=n.findIndex(t=>e.id===t);return(n.length-t)*-1}return r.includes(e.id)?1+r.findIndex(t=>e.id===t):0}}var K=class{#e;#t;constructor({page:e,numItemsPerPage:t},n){this.#e=e??1,this.numItemsPerPage=t,this.#t=n}setPage(e){if(e!==this.#e){let t=this.#e;this.#e=e,this.#t.touch({source:F.PAGINATION,event:P.SET_PAGE,current:{page:this.#e},initial:{page:t}})}}setNumItemsPerPage(e){if(e!==this.numItemsPerPage){let t=this.numItemsPerPage;this.numItemsPerPage=e,this.#t.touch({source:F.PAGINATION,event:P.SET_NUM_ITEMS_PER_PAGE,current:{numItemsPerPage:this.numItemsPerPage},initial:{numItemsPerPage:t}})}}reset(){this.#e=1}get lastPage(){if(this.numItemsPerPage!==void 0)return Math.ceil(this.#t.getItems().length/this.numItemsPerPage)}get numTotalItems(){return this.#t.getItems().length}get page(){return this.numItemsPerPage&&this.lastPage?s(this.#e,1,this.lastPage):this.#e}get offset(){return this.numItemsPerPage&&this.lastPage?(s(this.#e,1,this.lastPage)-1)*this.numItemsPerPage:0}serialize(){return{page:this.#e,numItemsPerPage:this.numItemsPerPage}}get api(){return{page:this.page,offset:this.offset,numItemsPerPage:this.numItemsPerPage,numTotalItems:this.numTotalItems,lastPage:this.lastPage,isPaginated:this.numItemsPerPage!==void 0,setPage:this.setPage.bind(this),setNumItemsPerPage:this.setNumItemsPerPage.bind(this)}}static process(e,t){if(e.numItemsPerPage===void 0)return t;let n=Math.ceil(t.length/e.numItemsPerPage),r=(s(e.page,1,n)-1)*e.numItemsPerPage;return t.slice(r,r+e.numItemsPerPage)}};function be(e,t){let n=e.length/t.length,r=1,i=1,a=0;for(let t=0;t<e.length;t+=1)a!==void 0&&e.at(t)===a+1&&(i+=1,i>=r&&(r=i)),a=e.at(t);return{percentOfHaystackMatched:n,longestSequentialSequence:r}}function q(e,t,n){let r=t.reduce((t,r)=>{if(e.rule?.searchFn===void 0)return t;let i=e.rule.searchFn(r,n),a=(Array.isArray(i)?i.map(j):[j(i)]).reduce((t,n)=>{let r=M(n,e.searchTerm);return r!==void 0&&t.push(be(r,n)),t},[]);if(a.length>0){let e=u(a,[`percentOfHaystackMatched`,`longestSequentialSequence`],[`desc`,`asc`]).at(0);e&&t.push({item:r,score:e})}return t},[]),i=r.reduce((e,t)=>(t.score.longestSequentialSequence>e&&(e=t.score.longestSequentialSequence),e),0);return u(r,[e=>e.score.percentOfHaystackMatched*100+e.score.longestSequentialSequence/i*100],[`desc`]).map(e=>e.item)}var J=class{#e;constructor({initialSearchTerm:e},t){if(this.searchTerm=``,e&&t.getRuleBook().rules.find(z)===void 0)throw new L(I.NO_SEARCH_RULE_SET);this.searchTerm=e??``,this.#e=t}get rule(){return this.#e.getRuleBook().rules.find(z)}get hasSearchRule(){return this.rule!==void 0}get hasSearchTerm(){return this.searchTerm!==``}get suggestedFilters(){let e=[];if(this.hasSearchTerm&&this.rule?.suggestFilters){let t=this.#e.getRuleBook().rules.filter(V),n=t.filter(e=>e.options&&e.options.length>0);n.length>0&&n.forEach(t=>{let n=q({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},t.options??[]);n.length>0&&e.push({rule:t,optionMatches:n})});let r=t.filter(e=>e.label!==void 0).filter(t=>e.some(e=>e.rule===t)===!1);r.length>0&&q({searchTerm:this.searchTerm,rule:{searchFn:e=>e.label}},r).forEach(t=>{e.push({rule:t})})}return e}setSearchTerm(e){let t=this.rule;if(!t)throw new L(I.NO_SEARCH_RULE_SET);if(typeof e!=`string`)throw new L(I.INVALID_SEARCH_TERM_TYPE);if(this.#e.isDisabled())return;let n=this.searchTerm;this.searchTerm=e,n!==e&&this.#e.debouncer(t,()=>{this.#e.touch({source:F.SEARCH,event:P.SET_SEARCH_TERM,current:e,initial:n,rule:t})})}reset(){if(this.#e.isDisabled())return;let e=this.searchTerm;this.searchTerm=``,this.#e.touch({source:F.SEARCH,event:P.RESET_SEARCH_TERM,current:``,initial:e,rule:this.rule})}serialize(){return{searchTerm:this.searchTerm,rule:this.rule}}test(e,t=!1){return this.#e.test({search:{searchTerm:e,rule:this.rule}},t)}get api(){return{rule:this.rule,searchTerm:this.searchTerm,suggestedFilters:this.suggestedFilters,hasSearchTerm:this.hasSearchTerm,hasSearchRule:this.hasSearchRule,setSearchTerm:this.setSearchTerm.bind(this),reset:this.reset.bind(this),test:this.test.bind(this)}}static process(e,t,n){if(e.rule===void 0)throw new L(I.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:q(e,t,n)}};const Y=[void 0,`desc`,`asc`];var X=class{#e;#t;#n;constructor({initialSortBy:e,initialSortDirection:t},n){this.#n=n,e&&(this.#e=this.getRule(e)),this.#t=t}getRule(e){let t=this.#n.getRuleBook().getRule(e);if(R(t)===!1)throw new L(I.WRONG_RULE_TYPE_FOR_MIXIN,{rule:t});return t}get rules(){return this.#n.getRuleBook().rules.filter(R)}get activeRule(){let e=this.rules.at(0);return this.#e??e}get sortDirection(){return this.#t??this.activeRule?.defaultSortDirection??`asc`}get userHasSetSortDirection(){return this.#t!==void 0}setSortDirection(e){if(this.#n.isDisabled()||!this.activeRule)return;let t=this.#t;this.#t=e,this.#n.touch({source:F.SORT_BY,event:P.SET_SORT_BY_DIRECTION,current:{sortDirection:e},initial:{sortDirection:t},rule:this.activeRule})}cycleSortDirection(){let e=Y.findIndex(e=>e===this.#t);if(e!==-1){let t=e+1%(Y.length-1);this.setSortDirection(Y[t])}}toggleSortDirection(){if((this.#t??this.activeRule?.defaultSortDirection)===`desc`){this.setSortDirection(`asc`);return}this.setSortDirection(`desc`)}set(e,t){if(this.#n.isDisabled()||!this.activeRule)return;let n=this.#t,r=this.#e,i=e?this.getRule(e):void 0;this.#e=i,this.#t=t,this.#n.touch({source:F.SORT_BY,event:P.SET_SORT_BY,current:{rule:i?.id,sortDirection:t},initial:{rule:r?.id,sortDirection:n},rule:this.activeRule})}toggle(e){if(!this.#n.isDisabled()){if(this.isRuleActive(e)){this.toggleSortDirection();return}this.set(e)}}isRuleActive(e){return this.getRule(e).id===this.activeRule?.id}reset(){this.set(void 0,void 0)}serialize(){return{rule:this.activeRule,sortDirection:this.sortDirection}}get api(){return{activeRule:this.activeRule,sortDirection:this.sortDirection,userHasSetSortDirection:this.userHasSetSortDirection,rules:this.rules,set:this.set.bind(this),toggle:this.toggle.bind(this),isRuleActive:this.isRuleActive.bind(this),reset:this.reset.bind(this),setSortDirection:this.setSortDirection.bind(this),cycleSortDirection:this.cycleSortDirection.bind(this),toggleSortDirection:this.toggleSortDirection.bind(this)}}static process(e,t,n){return e.rule===void 0?t:u(t,(Array.isArray(e.rule.sortFn)?e.rule.sortFn:[e.rule.sortFn]).map(e=>t=>e(t,n)),e.sortDirection)}},Z=class e{constructor(){this.snapshot={},this.isStale=!0}setIsStale(e){this.isStale=e}takeSnapshot({items:t,context:n,mixins:r}){let i=e.test({mixins:r,items:t,context:n}),a=r.pagination?K.process(r.pagination,i):i,o=[],s=r.groupBy!==void 0;r.groupBy&&(o=G.process(r.groupBy,a,n)),this.snapshot={items:s?void 0:a,groups:s?o:void 0,numMatchedItems:i.length,numTotalItems:t.length,hasGroupByRule:s}}static test({mixins:e,items:t,context:n}){let r=[...t];return e.search&&(r=J.process(e.search,r,n)),e.filters&&(r=W.process(e.filters,r,n)),e.sortBy&&(r=X.process(e.sortBy,r,n)),r}},xe=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.rules==`function`?n.rules(e,t):n.rules,i=Array.isArray(r)?r:[r];return{...n,rules:i,_isHydrated:!0}})}setEffects(e){this.#e=e}onChange(e,t){this.effects.forEach(n=>{n.rules.some(t=>typeof t==`string`&&e.id===t||typeof t==`object`&&e.id===t.id)&&n.onChange(t,e)})}},Se=class e{constructor(t){this.rules=[],e.validateDefinitions(t),this.definitions=t}hydrateDefinitions(e,t){this.rules=this.definitions.map(n=>{if(B(n)){let r=typeof n.options==`function`?n.options({items:e,context:t}):n.options;return{...n,boolean:!!n.boolean,multiple:!!n.multiple,strictOptions:n.strictOptions??!0,options:r}}return n})}getRule(e){let t=this.rules.find(t=>typeof e==`object`?t.id===e.id:t.id===e);if(t===void 0)throw new L(I.RULE_NOT_FOUND,e);return t}setRules(t){e.validateDefinitions(t),this.definitions=t}static validateDefinitions(e){if(e.length===0)return!1;let t=[z,B,R,H],n=new Set;return e.forEach(e=>{if(e.id===void 0&&!z(e))throw new L(I.INVALID_RULE_WITHOUT_ID,e);if(t.some(t=>t(e))===!1)throw new L(I.INVALID_RULE_SHAPE,e);if(e.id){if(n.has(e.id))throw new L(I.INVALID_RULE_DUPLICATE,e);n.add(e.id)}}),!0}},Ce=class{#e;constructor(e){this.effects=[],this.#e=e}hydrateDefinitions(e,t){this.effects=this.#e.map(n=>{let r=typeof n.haystack==`function`?n.haystack(e,t):n.haystack,i=Array.isArray(r)?r:[r];return{...n,haystack:i}})}setEffects(e){this.#e=e}processSearchTerm(e,t){this.effects.forEach(n=>{se(n.haystack,e)&&n.onChange(t,e)})}},we=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new Se(e),this.ruleEffects=new xe(t),this.searchEffects=new Ce(n)}hydrateDefinitions(e,t){this.list.hydrateDefinitions(e,t),this.searchEffects.hydrateDefinitions(e,t),this.ruleEffects.hydrateDefinitions(e,t)}onChange(e,t){z(e)&&t.search.hasSearchTerm&&this.searchEffects.processSearchTerm(t.search.searchTerm,t),this.ruleEffects.onChange(e,t)}},Q=class{#e;#t;#n;#r;#i;#a;constructor(e,{rules:t,effects:n,initialSearchTerm:r,initialSortBy:i,initialSortDirection:a,initialGroupBy:o,initialGroupBySortDirection:s,initialFilters:c,context:l,page:u,numItemsPerPage:d,resetPaginationOn:f=[`sortBy`,`groupBy`],isLoading:p,disabled:m,requireGroup:h,ignoreSortByRulesWhileSearchRuleIsActive:g,onInit:_,onReady:v,onFirstUserInteraction:y,onChange:b},x){this.isReady=!1,this.#t=!1,this.#r=new ue,this.#i=new Z,this.#e=e,this.disabled=!!m,this.isLoading=!!p,this.isReady=!p&&Array.isArray(e),this.getPublicInterfaceFn=x,this.updatedAt=Date.now(),this.context=l,this.#n=g,this.resetPaginationOn=f;let S=n?.filter(de)??[],C=n?.filter(fe)??[];this.#a=new we({rules:t,ruleEffects:S,searchEffects:C}),this.#a.hydrateDefinitions(e??[],l);let w={getItems:()=>this.items,getRuleBook:()=>this.#a.list,isLoading:()=>this.isLoading,isDisabled:()=>this.disabled,test:(e,t)=>this.test(e,t),touch:e=>this.#o(e),debouncer:le()};this.search=new J({initialSearchTerm:r},w),this.filters=new W({initialFilters:c},w),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},w),this.groupBy=new G({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!h},w),this.pagination=new K({page:u,numItemsPerPage:d},w),this.#r.silently(()=>{let e={source:F.CORE,event:P.INIT,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};_&&_(e)}),b&&this.#r.on(P.CHANGE,b),y&&this.#r.on(P.FIRST_USER_INTERACTION,y),v&&(this.isReady?v({source:F.CORE,event:P.READY,timestamp:Date.now(),instance:this.getPublicInterfaceFn()}):this.#r.on(P.READY,v))}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0),(this.resetPaginationOn.includes(`change`)||this.resetPaginationOn.some(t=>t===e.source))&&this.pagination.reset();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(P.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getPublicInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(e.event,t)}#c(){this.updatedAt=Date.now(),this.#r.emit(P.SYNC_UPDATED_AT,this.updatedAt)}emitFirstUserInteraction(){if(this.#t===!1){this.#t=!0;let e={source:F.CORE,event:P.FIRST_USER_INTERACTION,timestamp:Date.now(),instance:this.getPublicInterfaceFn()};this.#r.emit(P.FIRST_USER_INTERACTION,e)}}#l(){this.isReady===!1&&(this.isReady=!0,this.#r.emit(P.READY,{source:F.CORE,event:P.READY,timestamp:Date.now()}))}get items(){return Array.isArray(this.#e)?this.#e:[]}get matches(){return this.#i.isStale&&(this.#i.takeSnapshot({items:this.items,context:this.context,mixins:this.#u()}),this.#i.setIsStale(!1)),this.#i.snapshot}test(e,t=!1){if(t){let t={...this.#u(),...e};return Z.test({mixins:t,items:this.items,context:this.context})}return Z.test({mixins:e,items:this.items,context:this.context})}#u(){let e=this.search.hasSearchRule&&this.search.hasSearchTerm,t=e&&this.#n,n={};return e&&(n.search=this.search.serialize()),this.filters.activeRules.length>0&&(n.filters=this.filters.serialize()),this.pagination.numItemsPerPage&&(n.pagination=this.pagination.serialize()),t===!1&&(n.sortBy=this.sortBy.serialize()),this.groupBy.activeRule!==void 0&&(n.groupBy=this.groupBy.serialize()),n}get isEmpty(){return this.isLoading===!1&&this.items.length===0}get hasMatches(){let e=Array.isArray(this.matches.items)&&this.matches.items.length>0,t=Array.isArray(this.matches.groups)&&this.matches.groups.length>0;return e||t}get events(){return{on:(e,t)=>this.#r.on(e,t),off:(e,t)=>this.#r.off(e,t),silently:e=>this.#r.silently(e),isSilent:()=>this.#r.isSilent()}}getRule(e){return this.#a.list.getRule(e)}get state(){if(this.isLoading)return`loading`;if(this.isEmpty)return`empty`;let e=this.groupBy.activeRule!==void 0;return e&&Array.isArray(this.matches.groups)&&this.matches.groups.length>0?`groups`:e===!1&&Array.isArray(this.matches.items)&&this.matches.items.length>0?`items`:`noMatches`}setItems(e){if(l(e,this.#e)===!1){let t=this.#e;this.#e=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:F.CORE,event:P.SET_ITEMS,current:e,initial:t})}}setIsLoading(e){if(!!e!==this.isLoading){let t=this.isLoading;this.isLoading=!!e,this.#s({source:F.CORE,event:P.SET_IS_LOADING,current:!!e,initial:t}),this.isLoading===!1&&this.#l()}}setIsDisabled(e){if(!!e!==this.disabled){let t=this.disabled;this.disabled=!!e,this.#s({source:F.CORE,event:P.SET_IS_DISABLED,current:!!e,initial:t})}}setRules(e){l(e,this.#a.list.definitions)===!1&&(this.#a.list.setRules(e),this.#a.list.hydrateDefinitions(this.items,this.context))}setContext(e){let t=this.context;l(e,t)===!1&&(this.context=e,this.#a.hydrateDefinitions(this.items,this.context),this.#s({source:F.CORE,event:P.SET_CONTEXT,current:e,initial:t}))}toJSON(){return{disabled:this.disabled,initialSearchTerm:this.search.searchTerm,initialFilters:this.filters.raw,page:this.pagination.page,numItemsPerPage:this.pagination.numItemsPerPage,initialSortBy:this.sortBy.activeRule?.id,initialSortDirection:this.sortBy.sortDirection,ignoreSortByRulesWhileSearchRuleIsActive:this.#n,initialGroupBy:this.groupBy.activeRule?.id,initialGroupBySortDirection:this.groupBy.groupBySortDirection,requireGroup:this.groupBy.requireGroup,resetPaginationOn:this.resetPaginationOn}}},Te=class{#e;constructor(e,t){let n=()=>this;this.#e=new Q(e,t,n)}get items(){return this.#e.items}get context(){return this.#e.context}get isReady(){return this.#e.isReady}get isEmpty(){return this.#e.isEmpty}get hasMatches(){return this.#e.hasMatches}get isLoading(){return this.#e.isLoading}get disabled(){return this.#e.disabled}get state(){return this.#e.state}get updatedAt(){return this.#e.updatedAt}get events(){return this.#e.events}get matches(){return this.#e.matches}get search(){return this.#e.search.api}get filters(){return this.#e.filters.api}get sortBy(){return this.#e.sortBy.api}get groupBy(){return this.#e.groupBy.api}get pagination(){return this.#e.pagination.api}setItems(e){return this.#e.setItems(e)}setIsLoading(e){return this.#e.setIsLoading(e)}setIsDisabled(e){return this.#e.setIsDisabled(e)}setRules(e){return this.#e.setRules(e)}setContext(e){return this.#e.setContext(e)}test(e,t=!1){return this.#e.test(e,t)}getRule(e){return this.#e.getRule(e)}toJSON(){return this.#e.toJSON()}};function $({items:e,rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w,controllerRef:T,children:E}){let[D,O]=a(Date.now()),[k]=a(()=>{let n=new Te(e,{rules:t,effects:i,initialSearchTerm:s,initialSortBy:c,initialSortDirection:l,initialGroupBy:u,initialGroupBySortDirection:d,initialFilters:f,context:p,isLoading:m,disabled:h,page:g,numItemsPerPage:_,requireGroup:v,ignoreSortByRulesWhileSearchRuleIsActive:y,onInit:b,onReady:x,onFirstUserInteraction:S,onChange:w});return n.events.on(P.SYNC_UPDATED_AT,e=>O(e)),n});k.setItems(e),k.setIsLoading(m),k.setIsDisabled(h),k.setRules(t),p!==void 0&&k.setContext(p),g!==void 0&&k.pagination.setPage(g),_!==void 0&&k.pagination.setNumItemsPerPage(_),n(T,()=>k,[k]);let A=r(()=>[k,D],[k,D]);return o(C.Provider,{value:A,children:E})}$.Content=A,$.SearchTermHaystack=ce;function Ee(){return i(null)}export{$ as Finder,N as StringMatch,h as filterRule,p as finderRuleset,_ as groupByRule,v as ruleEffect,y as searchEffect,m as searchRule,g as sortByRule,x as transformFilterToBoolean,S as transformFilterToMultiple,b as transformFilterToSingleValue,w as useFinder,Ee as useFinderRef};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hitgrab/finder",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Effortless data manipulation with static rules.",
5
5
  "type": "module",
6
6
  "files": [