const p=Symbol.for("@ts-pattern/matcher"),N=Symbol.for("@ts-pattern/isVariadic"),x="@ts-pattern/anonymous-select-key",B=e=>!!(e&&typeof e=="object"),O=e=>e&&!!e[p],g=(e,t,r)=>{if(O(e)){const n=e[p](),{matched:i,selections:s}=n.match(t);return i&&s&&Object.keys(s).forEach(a=>r(a,s[a])),i}if(B(e)){if(!B(t))return!1;if(Array.isArray(e)){if(!Array.isArray(t))return!1;let n=[],i=[],s=[];for(const a of e.keys()){const h=e[a];O(h)&&h[N]?s.push(h):s.length?i.push(h):n.push(h)}if(s.length){if(s.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(t.length<n.length+i.length)return!1;const a=t.slice(0,n.length),h=i.length===0?[]:t.slice(-i.length),K=t.slice(n.length,i.length===0?1/0:-i.length);return n.every((w,j)=>g(w,a[j],r))&&i.every((w,j)=>g(w,h[j],r))&&(s.length===0||g(s[0],K,r))}return e.length===t.length&&e.every((a,h)=>g(a,t[h],r))}return Reflect.ownKeys(e).every(n=>{const i=e[n];return(n in t||O(s=i)&&s[p]().matcherType==="optional")&&g(i,t[n],r);var s})}return Object.is(t,e)},b=e=>{var t,r,n;return B(e)?O(e)?(t=(r=(n=e[p]()).getSelectionKeys)==null?void 0:r.call(n))!=null?t:[]:Array.isArray(e)?S(e,b):S(Object.values(e),b):[]},S=(e,t)=>e.reduce((r,n)=>r.concat(t(n)),[]);function c(e){return Object.assign(e,{optional:()=>W(e),and:t=>u(e,t),or:t=>I(e,t),select:t=>t===void 0?T(e):T(t,e)})}function W(e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return t===void 0?(b(e).forEach(i=>n(i,void 0)),{matched:!0,selections:r}):{matched:g(e,t,n),selections:r}},getSelectionKeys:()=>b(e),matcherType:"optional"})})}function u(...e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return{matched:e.every(i=>g(i,t,n)),selections:r}},getSelectionKeys:()=>S(e,b),matcherType:"and"})})}function I(...e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return S(e,b).forEach(i=>n(i,void 0)),{matched:e.some(i=>g(i,t,n)),selections:r}},getSelectionKeys:()=>S(e,b),matcherType:"or"})})}function o(e){return{[p]:()=>({match:t=>({matched:!!e(t)})})}}function T(...e){const t=typeof e[0]=="string"?e[0]:void 0,r=e.length===2?e[1]:typeof e[0]=="string"?void 0:e[0];return c({[p]:()=>({match:n=>{let i={[t??x]:n};return{matched:r===void 0||g(r,n,(s,a)=>{i[s]=a}),selections:i}},getSelectionKeys:()=>[t??x].concat(r===void 0?[]:b(r))})})}function k(e){return!0}function l(e){return typeof e=="number"}function y(e){return typeof e=="string"}function m(e){return typeof e=="bigint"}c(o(k));c(o(k));const v=e=>Object.assign(c(e),{startsWith:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.startsWith(r)))));var r},endsWith:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.endsWith(r)))));var r},minLength:t=>v(u(e,(r=>o(n=>y(n)&&n.length>=r))(t))),length:t=>v(u(e,(r=>o(n=>y(n)&&n.length===r))(t))),maxLength:t=>v(u(e,(r=>o(n=>y(n)&&n.length<=r))(t))),includes:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.includes(r)))));var r},regex:t=>{return v(u(e,(r=t,o(n=>y(n)&&!!n.match(r)))));var r}});v(o(y));const f=e=>Object.assign(c(e),{between:(t,r)=>f(u(e,((n,i)=>o(s=>l(s)&&n<=s&&i>=s))(t,r))),lt:t=>f(u(e,(r=>o(n=>l(n)&&n<r))(t))),gt:t=>f(u(e,(r=>o(n=>l(n)&&n>r))(t))),lte:t=>f(u(e,(r=>o(n=>l(n)&&n<=r))(t))),gte:t=>f(u(e,(r=>o(n=>l(n)&&n>=r))(t))),int:()=>f(u(e,o(t=>l(t)&&Number.isInteger(t)))),finite:()=>f(u(e,o(t=>l(t)&&Number.isFinite(t)))),positive:()=>f(u(e,o(t=>l(t)&&t>0))),negative:()=>f(u(e,o(t=>l(t)&&t<0)))});f(o(l));const d=e=>Object.assign(c(e),{between:(t,r)=>d(u(e,((n,i)=>o(s=>m(s)&&n<=s&&i>=s))(t,r))),lt:t=>d(u(e,(r=>o(n=>m(n)&&n<r))(t))),gt:t=>d(u(e,(r=>o(n=>m(n)&&n>r))(t))),lte:t=>d(u(e,(r=>o(n=>m(n)&&n<=r))(t))),gte:t=>d(u(e,(r=>o(n=>m(n)&&n>=r))(t))),positive:()=>d(u(e,o(t=>m(t)&&t>0))),negative:()=>d(u(e,o(t=>m(t)&&t<0)))});d(o(m));c(o(function(e){return typeof e=="boolean"}));c(o(function(e){return typeof e=="symbol"}));c(o(function(e){return e==null}));c(o(function(e){return e!=null}));class L extends Error{constructor(t){let r;try{r=JSON.stringify(t)}catch{r=t}super(`Pattern matching error: no pattern matches value ${r}`),this.input=void 0,this.input=t}}const E={matched:!1,value:void 0};function F(e){return new A(e,E)}class A{constructor(t,r){this.input=void 0,this.state=void 0,this.input=t,this.state=r}with(...t){if(this.state.matched)return this;const r=t[t.length-1],n=[t[0]];let i;t.length===3&&typeof t[1]=="function"?i=t[1]:t.length>2&&n.push(...t.slice(1,t.length-1));let s=!1,a={};const h=(w,j)=>{s=!0,a[w]=j},K=!n.some(w=>g(w,this.input,h))||i&&!i(this.input)?E:{matched:!0,value:r(s?x in a?a[x]:a:this.input,this.input)};return new A(this.input,K)}when(t,r){if(this.state.matched)return this;const n=!!t(this.input);return new A(this.input,n?{matched:!0,value:r(this.input,this.input)}:E)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(t=P){return this.state.matched?this.state.value:t(this.input)}run(){return this.exhaustive()}returnType(){return this}narrow(){return this}}function P(e){throw new L(e)}export{F as M};
