@hitgrab/finder 0.1.24-alpha → 0.1.26-alpha
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 +1 -1
- package/dist/index.d.cts +14 -11
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
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:ee(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 ee(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 k(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 M(){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 te=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_ON_RULE_WITH_SINGLE_VALUE:`Finder could not toggle this filter rule option, as the rule does not allow multiple values.`,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 se(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 H(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(t,n){throw new I(F.TOGGLING_OPTION_ON_RULE_WITH_SINGLE_VALUE,{rule:e,value:t,optionValue:n})},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 H(e);if(ie(e))return se(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()===``?void 0:t;W(n).validate(t),!(this.#e[n.id]!==void 0&&this.#e[n.id]===i)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:i},this.#t.touch({source:P.FILTERS,event:N.SET_FILTER,current:i,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);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}}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.#t}}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=[];return o&&e.rule&&(s.push(ce(e.rule)),c.push(`asc`)),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(e){let t=[];e.sticky?.header!==void 0&&(Array.isArray(e.sticky.header)&&(t=e.sticky.header),typeof e.sticky.header==`string`&&(t=[e.sticky.header]));let n=[];return e.sticky?.footer!==void 0&&(Array.isArray(e.sticky.footer)&&(n=e.sticky.footer),typeof e.sticky.footer==`string`&&(n=[e.sticky.footer])),e=>{if(t.includes(e.id)){let n=t.findIndex(t=>e.id===t);return(t.length-n)*-1}return n.includes(e.id)?1+n.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}})}}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}}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 ue(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 J=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)}static process(e,t,n){if(e.rule===void 0)throw new I(F.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:ue(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(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=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: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}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,t=>typeof e.rule?.sortFn==`function`?e.rule.sortFn(t,r):-1/0,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}},de=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)})}},fe=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}},pe=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=>{k(n.haystack,e)&&n.onChange(t,e)})}},Q=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new fe(e),this.ruleEffects=new de(t),this.searchEffects=new pe(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=!1;#n;#r=new te;#i=new Z;#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,isLoading:f,disabled:p,requireGroup:m,ignoreSortByRulesWhileSearchRuleIsActive:h,onInit:g,onReady:_,onFirstUserInteraction:v,onChange:y},b){this.isReady=!1,this.#e=e,this.disabled=!!p,this.isLoading=!!f,this.isReady=!f&&Array.isArray(e)&&e.length>0,this.getInstanceInterfaceFn=b,this.updatedAt=Date.now(),this.context=l,this.#n=h,this.#a=new Q({rules:t,ruleEffects:n?.filter(ne)??[],searchEffects:n?.filter(re)??[]}),this.#a.hydrateDefinitions(e??[],l);let x={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:M()};this.search=new J({initialSearchTerm:r},x),this.filters=new G({initialFilters:c},x),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},x),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!m},x),this.pagination=new q({page:u,numItemsPerPage:d},x),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};g&&g(e)}),y&&this.#r.on(N.CHANGE,y),v&&this.#r.on(N.FIRST_USER_INTERACTION,v),_&&this.isReady&&_({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()}),this.isReady===!1&&_&&this.#r.on(N.READY,_)}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0);let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getInstanceInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};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.getInstanceInterfaceFn()};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 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((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}}},he=class{#e;constructor(e,t){this.#e=new me(e,t,()=>this)}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(){let e=this.#e.search;return{rule:e.rule,searchTerm:e.searchTerm,hasSearchTerm:e.hasSearchTerm,hasSearchRule:e.hasSearchRule,setSearchTerm:e.setSearchTerm.bind(e),reset:e.reset.bind(e),test:e.test.bind(e)}}get filters(){let e=this.#e.filters;return{values:e.values,raw:e.raw,activeRules:e.activeRules,rules:e.rules,isActive:e.isRuleActive.bind(e),get:e.get.bind(e),add:e.add.bind(e),has:e.has.bind(e),getRule:e.getRule.bind(e),toggle:e.toggle.bind(e),set:e.set.bind(e),delete:e.delete.bind(e),reset:e.reset.bind(e),test:e.test.bind(e),testRule:e.testRule.bind(e),testRuleOptions:e.testRuleOptions.bind(e)}}get sortBy(){let e=this.#e.sortBy;return{activeRule:e.activeRule,sortDirection:e.sortDirection,userHasSetSortDirection:e.userHasSetSortDirection,rules:e.rules,set:e.set.bind(e),toggle:e.toggle.bind(e),isRuleActive:e.isRuleActive.bind(e),reset:e.reset.bind(e),setSortDirection:e.setSortDirection.bind(e),cycleSortDirection:e.cycleSortDirection.bind(e),toggleSortDirection:e.toggleSortDirection.bind(e)}}get groupBy(){let e=this.#e.groupBy;return{activeRule:e.activeRule,requireGroup:e.requireGroup,rules:e.rules,groupBySortDirection:e.groupBySortDirection,set:e.set.bind(e),toggle:e.toggle.bind(e),setGroupSortDirection:e.setGroupSortDirection.bind(e),reset:e.reset.bind(e)}}get pagination(){let e=this.#e.pagination;return{page:e.page,offset:e.offset,numItemsPerPage:e.numItemsPerPage,numTotalItems:e.numTotalItems,lastPage:e.lastPage,isPaginated:e.numItemsPerPage!==void 0,setPage:e.setPage.bind(e),setNumItemsPerPage:e.setNumItemsPerPage.bind(e)}}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
|
+
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:ee(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 ee(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 k(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 M(){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 te=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_ON_RULE_WITH_SINGLE_VALUE:`Finder could not toggle this filter rule option, as the rule does not allow multiple values.`,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 se(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 H(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(t,n){throw new I(F.TOGGLING_OPTION_ON_RULE_WITH_SINGLE_VALUE,{rule:e,value:t,optionValue:n})},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 H(e);if(ie(e))return se(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()===``?void 0:t;W(n).validate(t),!(this.#e[n.id]!==void 0&&this.#e[n.id]===i)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:i},this.#t.touch({source:P.FILTERS,event:N.SET_FILTER,current:i,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);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}}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.#t}}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}})}}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}}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 ue(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 J=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)}static process(e,t,n){if(e.rule===void 0)throw new I(F.NO_SEARCH_RULE_SET);return e.searchTerm===``?t:ue(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(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=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: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}}static process(e,t,r){return e.rule===void 0?t:(0,n.orderBy)(t,t=>typeof e.rule?.sortFn==`function`?e.rule.sortFn(t,r):-1/0,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}},de=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)})}},fe=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}},pe=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=>{k(n.haystack,e)&&n.onChange(t,e)})}},Q=class{constructor({rules:e,ruleEffects:t,searchEffects:n}){this.list=new fe(e),this.ruleEffects=new de(t),this.searchEffects=new pe(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=!1;#n;#r=new te;#i=new Z;#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,isLoading:f,disabled:p,requireGroup:m,ignoreSortByRulesWhileSearchRuleIsActive:h,onInit:g,onReady:_,onFirstUserInteraction:v,onChange:y},b){this.isReady=!1,this.#e=e,this.disabled=!!p,this.isLoading=!!f,this.isReady=!f&&Array.isArray(e)&&e.length>0,this.getInstanceInterfaceFn=b,this.updatedAt=Date.now(),this.context=l,this.#n=h,this.#a=new Q({rules:t,ruleEffects:n?.filter(ne)??[],searchEffects:n?.filter(re)??[]}),this.#a.hydrateDefinitions(e??[],l);let x={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:M()};this.search=new J({initialSearchTerm:r},x),this.filters=new G({initialFilters:c},x),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},x),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!m},x),this.pagination=new q({page:u,numItemsPerPage:d},x),this.#r.silently(()=>{let e={source:P.CORE,event:N.INIT,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};g&&g(e)}),y&&this.#r.on(N.CHANGE,y),v&&this.#r.on(N.FIRST_USER_INTERACTION,v),_&&this.isReady&&_({source:P.CORE,event:N.READY,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()}),this.isReady===!1&&_&&this.#r.on(N.READY,_)}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0);let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};this.#r.emit(N.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getInstanceInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};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.getInstanceInterfaceFn()};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 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((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}}},he=class{#e;constructor(e,t){this.#e=new me(e,t,()=>this)}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(){let e=this.#e.search;return{rule:e.rule,searchTerm:e.searchTerm,hasSearchTerm:e.hasSearchTerm,hasSearchRule:e.hasSearchRule,setSearchTerm:e.setSearchTerm.bind(e),reset:e.reset.bind(e),test:e.test.bind(e)}}get filters(){let e=this.#e.filters;return{values:e.values,raw:e.raw,activeRules:e.activeRules,rules:e.rules,isActive:e.isRuleActive.bind(e),get:e.get.bind(e),add:e.add.bind(e),has:e.has.bind(e),getRule:e.getRule.bind(e),toggle:e.toggle.bind(e),set:e.set.bind(e),delete:e.delete.bind(e),reset:e.reset.bind(e),test:e.test.bind(e),testRule:e.testRule.bind(e),testRuleOptions:e.testRuleOptions.bind(e)}}get sortBy(){let e=this.#e.sortBy;return{activeRule:e.activeRule,sortDirection:e.sortDirection,userHasSetSortDirection:e.userHasSetSortDirection,rules:e.rules,set:e.set.bind(e),toggle:e.toggle.bind(e),isRuleActive:e.isRuleActive.bind(e),reset:e.reset.bind(e),setSortDirection:e.setSortDirection.bind(e),cycleSortDirection:e.cycleSortDirection.bind(e),toggleSortDirection:e.toggleSortDirection.bind(e)}}get groupBy(){let e=this.#e.groupBy;return{activeRule:e.activeRule,requireGroup:e.requireGroup,rules:e.rules,groupBySortDirection:e.groupBySortDirection,set:e.set.bind(e),toggle:e.toggle.bind(e),setGroupSortDirection:e.setGroupSortDirection.bind(e),reset:e.reset.bind(e)}}get pagination(){let e=this.#e.pagination;return{page:e.page,offset:e.offset,numItemsPerPage:e.numItemsPerPage,numTotalItems:e.numTotalItems,lastPage:e.lastPage,isPaginated:e.numItemsPerPage!==void 0,setPage:e.setPage.bind(e),setNumItemsPerPage:e.setNumItemsPerPage.bind(e)}}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,6 +1,6 @@
|
|
|
1
1
|
import * as react0 from "react";
|
|
2
2
|
import { ElementType, PropsWithChildren, ReactElement, ReactNode, RefObject } from "react";
|
|
3
|
-
import * as
|
|
3
|
+
import * as react_jsx_runtime5 from "react/jsx-runtime";
|
|
4
4
|
|
|
5
5
|
//#region src/core/types/rule-types.d.ts
|
|
6
6
|
/**
|
|
@@ -74,7 +74,10 @@ interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
|
74
74
|
sticky?: {
|
|
75
75
|
header?: string | string[];
|
|
76
76
|
footer?: string | string[];
|
|
77
|
-
}
|
|
77
|
+
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
78
|
+
header?: string | string[];
|
|
79
|
+
footer?: string | string[];
|
|
80
|
+
});
|
|
78
81
|
}
|
|
79
82
|
interface FilterTestOptions {
|
|
80
83
|
rules: HydratedFilterRuleDefinition[];
|
|
@@ -420,7 +423,7 @@ interface FinderContentLoadingProps<FItem, FContext> {
|
|
|
420
423
|
}
|
|
421
424
|
declare function FinderContentLoading<FItem, FContext>({
|
|
422
425
|
children: Component
|
|
423
|
-
}: FinderContentLoadingProps<FItem, FContext>):
|
|
426
|
+
}: FinderContentLoadingProps<FItem, FContext>): react_jsx_runtime5.JSX.Element | Iterable<ReactNode> | null;
|
|
424
427
|
//#endregion
|
|
425
428
|
//#region src/react/components/finder-content-empty.d.ts
|
|
426
429
|
interface FinderContentEmptyProps<FItem, FContext> {
|
|
@@ -428,7 +431,7 @@ interface FinderContentEmptyProps<FItem, FContext> {
|
|
|
428
431
|
}
|
|
429
432
|
declare function FinderContentEmpty<FItem = any, FContext = any>({
|
|
430
433
|
children: Component
|
|
431
|
-
}: FinderContentEmptyProps<FItem, FContext>):
|
|
434
|
+
}: FinderContentEmptyProps<FItem, FContext>): react_jsx_runtime5.JSX.Element | Iterable<ReactNode> | null;
|
|
432
435
|
//#endregion
|
|
433
436
|
//#region src/react/components/finder-content-no-matches.d.ts
|
|
434
437
|
interface FinderContentNoMatchesProps<FItem, FContext> {
|
|
@@ -436,7 +439,7 @@ interface FinderContentNoMatchesProps<FItem, FContext> {
|
|
|
436
439
|
}
|
|
437
440
|
declare function FinderContentNoMatches<FItem = any, FContext = any>({
|
|
438
441
|
children: Component
|
|
439
|
-
}: FinderContentNoMatchesProps<FItem, FContext>):
|
|
442
|
+
}: FinderContentNoMatchesProps<FItem, FContext>): react_jsx_runtime5.JSX.Element | Iterable<ReactNode> | null;
|
|
440
443
|
//#endregion
|
|
441
444
|
//#region src/react/components/finder-content-items.d.ts
|
|
442
445
|
interface FinderContentItemsProps<FItem, FContext> {
|
|
@@ -444,7 +447,7 @@ interface FinderContentItemsProps<FItem, FContext> {
|
|
|
444
447
|
}
|
|
445
448
|
declare function FinderContentItems<FItem = any, FContext = any>({
|
|
446
449
|
children: Component
|
|
447
|
-
}: FinderContentItemsProps<FItem, FContext>):
|
|
450
|
+
}: FinderContentItemsProps<FItem, FContext>): react_jsx_runtime5.JSX.Element | Iterable<ReactNode> | null;
|
|
448
451
|
//#endregion
|
|
449
452
|
//#region src/react/components/finder-content-groups.d.ts
|
|
450
453
|
interface FinderContentGroupsProps<FItem, FContext> {
|
|
@@ -452,7 +455,7 @@ interface FinderContentGroupsProps<FItem, FContext> {
|
|
|
452
455
|
}
|
|
453
456
|
declare function FinderContentGroups<FItem = any, FContext = any>({
|
|
454
457
|
children: Component
|
|
455
|
-
}: FinderContentGroupsProps<FItem, FContext>):
|
|
458
|
+
}: FinderContentGroupsProps<FItem, FContext>): react_jsx_runtime5.JSX.Element | Iterable<ReactNode> | null;
|
|
456
459
|
//#endregion
|
|
457
460
|
//#region src/react/components/finder-content.d.ts
|
|
458
461
|
interface FinderContentContainerProps<FItem, FContext> {
|
|
@@ -466,7 +469,7 @@ interface FinderContentContainerProps<FItem, FContext> {
|
|
|
466
469
|
}
|
|
467
470
|
declare function FinderContent<FItem = any, FContext = any>({
|
|
468
471
|
children: renderProps
|
|
469
|
-
}: FinderContentContainerProps<FItem, FContext>): (
|
|
472
|
+
}: FinderContentContainerProps<FItem, FContext>): (react_jsx_runtime5.JSX.Element | undefined)[];
|
|
470
473
|
declare namespace FinderContent {
|
|
471
474
|
var Loading: typeof FinderContentLoading;
|
|
472
475
|
var Empty: typeof FinderContentEmpty;
|
|
@@ -485,7 +488,7 @@ declare function FinderSearchTermHaystack({
|
|
|
485
488
|
Match,
|
|
486
489
|
Miss,
|
|
487
490
|
children: haystack
|
|
488
|
-
}: FinderSearchTermHaystackProps): string |
|
|
491
|
+
}: FinderSearchTermHaystackProps): string | react_jsx_runtime5.JSX.Element;
|
|
489
492
|
//#endregion
|
|
490
493
|
//#region src/react/components/finder.d.ts
|
|
491
494
|
declare function Finder<FItem = any, FContext = any>({
|
|
@@ -511,7 +514,7 @@ declare function Finder<FItem = any, FContext = any>({
|
|
|
511
514
|
onChange,
|
|
512
515
|
controllerRef,
|
|
513
516
|
children
|
|
514
|
-
}: FinderProps<FItem, FContext>):
|
|
517
|
+
}: FinderProps<FItem, FContext>): react_jsx_runtime5.JSX.Element;
|
|
515
518
|
declare namespace Finder {
|
|
516
519
|
var Content: typeof FinderContent;
|
|
517
520
|
var SearchTermHaystack: typeof FinderSearchTermHaystack;
|
|
@@ -535,6 +538,6 @@ declare function StringMatch({
|
|
|
535
538
|
haystack,
|
|
536
539
|
Match,
|
|
537
540
|
Miss
|
|
538
|
-
}: StringMatchProps): string | (string |
|
|
541
|
+
}: StringMatchProps): string | (string | react_jsx_runtime5.JSX.Element)[];
|
|
539
542
|
//#endregion
|
|
540
543
|
export { type AnyFilterRuleDefinition, type FilterOption, type FilterRuleDefinition, Finder, type FinderChangeEvent, type FinderConstructorOptions, type FinderContentProps, type FinderEvent, type FinderFirstUserInteractionEvent, type FinderInitEvent, type FinderProps, type FinderReadyEvent, type FinderResultGroup, type GroupByRuleDefinition, type RuleDefinition, type RuleEffect, type SearchEffect, type SearchRuleDefinition, type SortByRuleDefinition, type SortDirection, StringMatch, type StringMatchSegmentProps, filterRule, finderRuleset, groupByRule, ruleEffect, searchEffect, searchRule, sortByRule, transformFilterToBoolean, transformFilterToMultiple, transformFilterToSingleValue, useFinder, useFinderRef };
|
package/dist/index.d.mts
CHANGED
|
@@ -74,7 +74,10 @@ interface GroupByRuleDefinition<FItem = any, FContext = any> extends Rule {
|
|
|
74
74
|
sticky?: {
|
|
75
75
|
header?: string | string[];
|
|
76
76
|
footer?: string | string[];
|
|
77
|
-
}
|
|
77
|
+
} | ((groups: FinderResultGroup<FItem>[], context: FContext) => {
|
|
78
|
+
header?: string | string[];
|
|
79
|
+
footer?: string | string[];
|
|
80
|
+
});
|
|
78
81
|
}
|
|
79
82
|
interface FilterTestOptions {
|
|
80
83
|
rules: HydratedFilterRuleDefinition[];
|
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_ON_RULE_WITH_SINGLE_VALUE:`Finder could not toggle this filter rule option, as the rule does not allow multiple values.`,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(t,n){throw new R(L.TOGGLING_OPTION_ON_RULE_WITH_SINGLE_VALUE,{rule:e,value:t,optionValue:n})},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()===``?void 0:t;W(n).validate(t),!(this.#e[n.id]!==void 0&&this.#e[n.id]===i)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:i},this.#t.touch({source:I.FILTERS,event:F.SET_FILTER,current:i,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);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}}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.#t}}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=[];return a&&e.rule&&(o.push(ve(e.rule)),s.push(`asc`)),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(e){let t=[];e.sticky?.header!==void 0&&(Array.isArray(e.sticky.header)&&(t=e.sticky.header),typeof e.sticky.header==`string`&&(t=[e.sticky.header]));let n=[];return e.sticky?.footer!==void 0&&(Array.isArray(e.sticky.footer)&&(n=e.sticky.footer),typeof e.sticky.footer==`string`&&(n=[e.sticky.footer])),e=>{if(t.includes(e.id)){let n=t.findIndex(t=>e.id===t);return(t.length-n)*-1}return n.includes(e.id)?1+n.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}})}}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}}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)}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}}static process(e,t,n){return e.rule===void 0?t:u(t,t=>typeof e.rule?.sortFn==`function`?e.rule.sortFn(t,n):-1/0,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=!1;#n;#r=new le;#i=new Z;#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,isLoading:f,disabled:p,requireGroup:m,ignoreSortByRulesWhileSearchRuleIsActive:h,onInit:g,onReady:_,onFirstUserInteraction:v,onChange:y},b){this.isReady=!1,this.#e=e,this.disabled=!!p,this.isLoading=!!f,this.isReady=!f&&Array.isArray(e)&&e.length>0,this.getInstanceInterfaceFn=b,this.updatedAt=Date.now(),this.context=l,this.#n=h,this.#a=new we({rules:t,ruleEffects:n?.filter(ue)??[],searchEffects:n?.filter(de)??[]}),this.#a.hydrateDefinitions(e??[],l);let x={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},x),this.filters=new G({initialFilters:c},x),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},x),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!m},x),this.pagination=new q({page:u,numItemsPerPage:d},x),this.#r.silently(()=>{let e={source:I.CORE,event:F.INIT,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};g&&g(e)}),y&&this.#r.on(F.CHANGE,y),v&&this.#r.on(F.FIRST_USER_INTERACTION,v),_&&this.isReady&&_({source:I.CORE,event:F.READY,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()}),this.isReady===!1&&_&&this.#r.on(F.READY,_)}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0);let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};this.#r.emit(F.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getInstanceInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};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.getInstanceInterfaceFn()};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}}},Te=class{#e;constructor(e,t){this.#e=new Q(e,t,()=>this)}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(){let e=this.#e.search;return{rule:e.rule,searchTerm:e.searchTerm,hasSearchTerm:e.hasSearchTerm,hasSearchRule:e.hasSearchRule,setSearchTerm:e.setSearchTerm.bind(e),reset:e.reset.bind(e),test:e.test.bind(e)}}get filters(){let e=this.#e.filters;return{values:e.values,raw:e.raw,activeRules:e.activeRules,rules:e.rules,isActive:e.isRuleActive.bind(e),get:e.get.bind(e),add:e.add.bind(e),has:e.has.bind(e),getRule:e.getRule.bind(e),toggle:e.toggle.bind(e),set:e.set.bind(e),delete:e.delete.bind(e),reset:e.reset.bind(e),test:e.test.bind(e),testRule:e.testRule.bind(e),testRuleOptions:e.testRuleOptions.bind(e)}}get sortBy(){let e=this.#e.sortBy;return{activeRule:e.activeRule,sortDirection:e.sortDirection,userHasSetSortDirection:e.userHasSetSortDirection,rules:e.rules,set:e.set.bind(e),toggle:e.toggle.bind(e),isRuleActive:e.isRuleActive.bind(e),reset:e.reset.bind(e),setSortDirection:e.setSortDirection.bind(e),cycleSortDirection:e.cycleSortDirection.bind(e),toggleSortDirection:e.toggleSortDirection.bind(e)}}get groupBy(){let e=this.#e.groupBy;return{activeRule:e.activeRule,requireGroup:e.requireGroup,rules:e.rules,groupBySortDirection:e.groupBySortDirection,set:e.set.bind(e),toggle:e.toggle.bind(e),setGroupSortDirection:e.setGroupSortDirection.bind(e),reset:e.reset.bind(e)}}get pagination(){let e=this.#e.pagination;return{page:e.page,offset:e.offset,numItemsPerPage:e.numItemsPerPage,numTotalItems:e.numTotalItems,lastPage:e.lastPage,isPaginated:e.numItemsPerPage!==void 0,setPage:e.setPage.bind(e),setNumItemsPerPage:e.setNumItemsPerPage.bind(e)}}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 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_ON_RULE_WITH_SINGLE_VALUE:`Finder could not toggle this filter rule option, as the rule does not allow multiple values.`,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(t,n){throw new R(L.TOGGLING_OPTION_ON_RULE_WITH_SINGLE_VALUE,{rule:e,value:t,optionValue:n})},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()===``?void 0:t;W(n).validate(t),!(this.#e[n.id]!==void 0&&this.#e[n.id]===i)&&this.#t.debouncer(n,()=>{this.#e={...this.#e,[n.id]:i},this.#t.touch({source:I.FILTERS,event:F.SET_FILTER,current:i,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);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}}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.#t}}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}})}}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}}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)}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}}static process(e,t,n){return e.rule===void 0?t:u(t,t=>typeof e.rule?.sortFn==`function`?e.rule.sortFn(t,n):-1/0,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=!1;#n;#r=new le;#i=new Z;#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,isLoading:f,disabled:p,requireGroup:m,ignoreSortByRulesWhileSearchRuleIsActive:h,onInit:g,onReady:_,onFirstUserInteraction:v,onChange:y},b){this.isReady=!1,this.#e=e,this.disabled=!!p,this.isLoading=!!f,this.isReady=!f&&Array.isArray(e)&&e.length>0,this.getInstanceInterfaceFn=b,this.updatedAt=Date.now(),this.context=l,this.#n=h,this.#a=new we({rules:t,ruleEffects:n?.filter(ue)??[],searchEffects:n?.filter(de)??[]}),this.#a.hydrateDefinitions(e??[],l);let x={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},x),this.filters=new G({initialFilters:c},x),this.sortBy=new X({initialSortBy:i,initialSortDirection:a},x),this.groupBy=new K({initialGroupBy:o,initialGroupBySortDirection:s,requireGroup:!!m},x),this.pagination=new q({page:u,numItemsPerPage:d},x),this.#r.silently(()=>{let e={source:I.CORE,event:F.INIT,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};g&&g(e)}),y&&this.#r.on(F.CHANGE,y),v&&this.#r.on(F.FIRST_USER_INTERACTION,v),_&&this.isReady&&_({source:I.CORE,event:F.READY,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()}),this.isReady===!1&&_&&this.#r.on(F.READY,_)}#o(e){if(this.#r.isSilent())return;this.emitFirstUserInteraction(),this.#c(),this.#i.setIsStale(!0);let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};this.#r.emit(F.CHANGE,t),this.#r.silently(()=>{e.rule&&this.#a.onChange(e.rule,this.getInstanceInterfaceFn())})}#s(e){this.#i.setIsStale(!0),this.#c();let t={...e,timestamp:Date.now(),instance:this.getInstanceInterfaceFn()};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.getInstanceInterfaceFn()};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}}},Te=class{#e;constructor(e,t){this.#e=new Q(e,t,()=>this)}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(){let e=this.#e.search;return{rule:e.rule,searchTerm:e.searchTerm,hasSearchTerm:e.hasSearchTerm,hasSearchRule:e.hasSearchRule,setSearchTerm:e.setSearchTerm.bind(e),reset:e.reset.bind(e),test:e.test.bind(e)}}get filters(){let e=this.#e.filters;return{values:e.values,raw:e.raw,activeRules:e.activeRules,rules:e.rules,isActive:e.isRuleActive.bind(e),get:e.get.bind(e),add:e.add.bind(e),has:e.has.bind(e),getRule:e.getRule.bind(e),toggle:e.toggle.bind(e),set:e.set.bind(e),delete:e.delete.bind(e),reset:e.reset.bind(e),test:e.test.bind(e),testRule:e.testRule.bind(e),testRuleOptions:e.testRuleOptions.bind(e)}}get sortBy(){let e=this.#e.sortBy;return{activeRule:e.activeRule,sortDirection:e.sortDirection,userHasSetSortDirection:e.userHasSetSortDirection,rules:e.rules,set:e.set.bind(e),toggle:e.toggle.bind(e),isRuleActive:e.isRuleActive.bind(e),reset:e.reset.bind(e),setSortDirection:e.setSortDirection.bind(e),cycleSortDirection:e.cycleSortDirection.bind(e),toggleSortDirection:e.toggleSortDirection.bind(e)}}get groupBy(){let e=this.#e.groupBy;return{activeRule:e.activeRule,requireGroup:e.requireGroup,rules:e.rules,groupBySortDirection:e.groupBySortDirection,set:e.set.bind(e),toggle:e.toggle.bind(e),setGroupSortDirection:e.setGroupSortDirection.bind(e),reset:e.reset.bind(e)}}get pagination(){let e=this.#e.pagination;return{page:e.page,offset:e.offset,numItemsPerPage:e.numItemsPerPage,numTotalItems:e.numTotalItems,lastPage:e.lastPage,isPaginated:e.numItemsPerPage!==void 0,setPage:e.setPage.bind(e),setNumItemsPerPage:e.setNumItemsPerPage.bind(e)}}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};
|