var v=class t{constructor(e){this._promise=e;this._promise=e}static fromPromise(e,r){let n=e.then(s=>new c(s)).catch(s=>new f(r(s)));return new t(n)}static fromSafePromise(e){let r=e.then(n=>new c(n));return new t(r)}static fromThrowable(e,r){return(...n)=>t.fromPromise(e(n),s=>r?r(s):s)}async unwrap(){let e=await this._promise;if(e.isErr())throw e.error;return e.value}async match(e,r){let n=await this._promise;return n.isErr()?r(n.error):e(n.value)}map(e){return new t(this._promise.then(r=>r.isErr()?new f(r.error):new c(e(r.value))))}mapAsync(e){return new t(this._promise.then(async r=>r.isErr()?g(r.error):new c(await e(r.value))))}mapErr(e){return new t(this._promise.then(r=>r.isErr()?new f(e(r.error)):new c(r.value)))}mapErrAsync(e){return new t(this._promise.then(async r=>r.isErr()?g(await e(r.error)):new c(r.value)))}andThen(e){return new t(this._promise.then(r=>r.isErr()?g(r.error):e(r.value).toAsync()))}andThenAsync(e){return new t(this._promise.then(r=>r.isErr()?g(r.error):e(r.value)))}nullableToOption(){return this.map(e=>e?h(e):y)}flatten(){return new t(this._promise.then(e=>e.flatten()))}flattenOption(e){return new t(this._promise.then(r=>r.flattenOption(e)))}flattenOptionOrDefault(e){return new t(this._promise.then(r=>r.flattenOptionOrDefault(e)))}matchOption(e,r){return new t(this._promise.then(n=>n.matchOption(e,r)))}matchOptionAndFlatten(e,r){return new t(this._promise.then(n=>n.matchOptionAndFlatten(e,r)))}then(e,r){return this._promise.then(e,r)}};function O(t){return new v(Promise.resolve(new c(t)))}function g(t){return new v(Promise.resolve(new f(t)))}var c=class t{constructor(e){this.value=e;this.value=e,Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}tag="ok";isErr(){return!1}ifErr(e){return this}isErrOrNone(){return this.value instanceof T}isOk(){return!0}ifOk(e){return e(this.value),this}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}unwrapErr(){return y}match(e,r){return e(this.value)}map(e){let r=e(this.value);return new t(r)}mapAsync(e){return v.fromSafePromise(e(this.value))}mapOption(e){return this.value instanceof T||this.value instanceof E?u(this.value.map(e)):u(h(e(this.value)))}andThen(e){return e(this.value)}andThenAsync(e){return e(this.value)}mapErr(e){return u(this.value)}mapErrAsync(e){return O(this.value)}flatten(){return Y(this)}flattenOption(e){return this.value instanceof T||this.value instanceof E?this.value.okOrElse(e):new t(this.value)}flattenOptionOr(e){return this.value instanceof T||this.value instanceof E?this.value.unwrapOr(e):new t(this.value)}matchOption(e,r){return this.value instanceof T||this.value instanceof E?u(this.value.match(e,r)):u(e(this.value))}toNullable(){return this.value}toAsync(){return O(this.value)}void(){return u()}toJSON(){return{tag:"ok",value:this.value}}},f=class t{constructor(e){this.error=e;this.error=e,Object.defineProperties(this,{tag:{writable:!1,configurable:!1,enumerable:!1}})}tag="err";isErr(){return!0}ifErr(e){return e(this.error),this}isOk(){return!1}ifOk(e){return this}isErrOrNone(){return!0}unwrap(){let e=`Tried to unwrap error: ${X(this.error)}`;throw new Error(e)}unwrapOr(e){return e}unwrapOrElse(e){return e()}unwrapErr(){return h(this.error)}match(e,r){return r(this.error)}map(e){return new t(this.error)}mapAsync(e){return g(this.error)}mapErr(e){return new t(e(this.error))}mapErrAsync(e){return v.fromPromise(new Promise(()=>{throw""}),()=>e(this.error))}mapOption(e){return i(this.error)}andThen(e){return new t(this.error)}andThenAsync(e){return new t(this.error).toAsync()}flatten(){return Y(this)}flattenOption(e){return new t(this.error)}flattenOptionOr(e){return new t(this.error)}matchOption(e,r){return i(this.error)}toNullable(){return null}toAsync(){return g(this.error)}void(){return i(this.error)}toJSON(){return{tag:"err",error:this.error}}};function u(t){return new c(t)}function i(t){return new f(t)}function ce(t,e){return(...r)=>{try{let n=t(...r);return u(n)}catch(n){return i(e?e(n):n)}}}function X(t){if(t instanceof Error)return t.message?t.message:"code"in t&&typeof t.code=="string"?t.code:"An unknown error occurred";if(typeof t=="string")return t;if(typeof t=="object"&&t!==null&&"message"in t){let e=t;return typeof e.message=="string"?e.message:String(e.message)}return"An unknown error occurred"}function Y(t){let e=t;for(;e instanceof c;)e=e.value;return e}var k=class extends Error{constructor(e){super(`Failed to parse ${e} as result`)}};function pe(t){let e;if(typeof t=="string")try{e=JSON.parse(t)}catch(n){return i(new k(X(n)))}else e=t;if(typeof e!="object"||e===null)return i(new k("Expected an object but received type ${typeof data}."));let r=e;if("tag"in r)switch(r.tag){case"ok":{if("value"in r)return u(r.value);break}case"err":{if("error"in r)return i(r.error);break}}return i(new k("Object does not contain 'tag' and 'value' or 'error' property"))}var E=class t{constructor(e){this.value=e;Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}tag="some";isSome(){return!0}ifSome(e){return e(this.value),this}isNone(){return!1}ifNone(e){return this}map(e){return new t(e(this.value))}flatMap(e){return e(this.value)}andThen(e){return e(this.value)}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}or(e){return this}orElse(e){return this}match(e,r){return e(this.value)}toString(){return`Some(${this.value})`}toJSON(){return{tag:"some",value:this.value}}toNullable(){return this.value}toBoolean(){return!0}okOrElse(e){return u(this.value)}},T=class t{tag="none";constructor(){Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}isSome(){return!1}ifSome(e){return this}isNone(){return!0}ifNone(e){return e(),this}map(e){return new t}andThen(e){return y}flatMap(e){return new t}unwrap(){throw new Error("Tried to unwrap a non-existent value")}unwrapOr(e){return e}unwrapOrElse(e){return e()}or(e){return e}orElse(e){return e()}match(e,r){return r()}toString(){return"None"}toJSON(){return{tag:"none"}}toNullable(){return null}toBoolean(){return!1}okOrElse(e){return i(e())}};function h(t){return new E(t)}var y=new T;function Te(t){return t?h(t):y}var U=class t extends Error{constructor(r,n){super(t.getBestMsg(n)||"Schema validation error");this.input=r;this.detail=n;this.name="SchemaValidationError"}type="SchemaValiationError";format(){return{input:this.input,error:t.formatDetail(this.detail)}}get msg(){return t.getBestMsg(this.detail)}static getBestMsg(r){switch(r.kind){case"typeMismatch":case"unexpectedProperties":case"missingProperties":case"general":case"unionValidation":return t.formatMsg(r);case"propertyValidation":case"arrayElement":return r.msg||t.getBestMsg(r.detail);default:return"Unknown error"}}static formatDetail(r){switch(r.kind){case"general":case"typeMismatch":return t.formatMsg(r);case"propertyValidation":return{[r.property]:r.msg||this.formatDetail(r.detail)};case"unexpectedProperties":case"missingProperties":{let n=r.msg||(r.kind==="unexpectedProperties"?"Property is not allowed in a strict schema object":"Property is required, but missing");return r.keys.reduce((s,l)=>(s[l]=n,s),{})}case"arrayElement":{let n={};return r.msg&&(n.msg=r.msg),n[`index_${r.index}`]=this.formatDetail(r.detail),n}case"unionValidation":{let n=r.details?.map(s=>this.formatDetail(s));return r.msg&&n.unshift("Msg: "+r.msg),n}default:return"Unknown error type"}}static formatMsg(r){if(r.msg||r.kind==="general")return r.msg||"Unknown error";switch(r.kind){case"typeMismatch":return`Expected ${r.expected}, but received ${r.received}`;case"unexpectedProperties":return`Properties not allowed: ${r.keys.join(", ")}`;case"missingProperties":return`Missing required properties: ${r.keys.join(", ")}`;case"unionValidation":return"Input did not match any union member";default:return"Unknown error"}}};function o(t,e){return new U(t,e)}var a=class{constructor(e){this.msg=e}checks=[];parse(e){return this.validateInput(e).andThen(r=>this.applyValidationChecks(r))}static validatePrimitive(e,r,n){let s=typeof e;return s===r?u(e):i(o(e,{kind:"typeMismatch",expected:r,received:s,msg:n}))}addCheck(e){return this.checks.push(e),this}applyValidationChecks(e){for(let r of this.checks){let n=r(e);if(n)return i(n)}return u(e)}static isNullishSchema(e){return e.parse(null).isOk()||e.parse(void 0).isOk()}},A=class t extends a{static emailRegex=/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;static ipRegex=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;validateInput(e){return a.validatePrimitive(e,"string",this.msg)}max(e,r){return this.addCheck(n=>{if(n.length>e)return o(n,{kind:"general",msg:r||`String must be at most ${e} characters long`})})}min(e,r){return this.addCheck(n=>{if(n.length{if(!e.test(n))return o(n,{kind:"general",msg:r||`String must match pattern ${String(e)}`})})}email(e){return this.regex(t.emailRegex,e||"String must be a valid email address")}ip(e){return this.regex(t.ipRegex,e||"String must be a valid ip address")}},P=class extends a{constructor(r,n){super(n);this.literal=r}validateInput(r){return a.validatePrimitive(r,"string",this.msg).andThen(n=>n===this.literal?u(n):i(o(r,{kind:"typeMismatch",expected:this.literal,received:n,msg:this.msg})))}},V=class extends a{validateInput(e){return a.validatePrimitive(e,"number",this.msg)}gt(e,r){return this.addCheck(n=>{if(n<=e)return o(n,{kind:"general",msg:r||`Number must be greater than ${e}`})})}gte(e,r){return this.addCheck(n=>{if(n{if(n>=e)return o(n,{kind:"general",msg:r||`Number must be less than ${e}`})})}lte(e,r){return this.addCheck(n=>{if(n>e)return o(n,{kind:"general",msg:r||`Number must be less than or equal to ${e}`})})}int(e){return this.addCheck(r=>{if(!Number.isInteger(r))return o(r,{kind:"general",msg:e||"Number must be an integer"})})}positive(e){return this.gt(0,e||"Number must be positive")}nonnegative(e){return this.gte(0,e||"Number must be nonnegative")}negative(e){return this.lt(0,e||"Number must be negative")}nonpositive(e){return this.lte(0,e||"Number must be nonpositive")}finite(e){return this.addCheck(r=>{if(!Number.isFinite(r))return o(r,{kind:"general",msg:e||"Number must be an integer"})})}safe(e){return this.addCheck(r=>{if(!Number.isSafeInteger(r))return o(r,{kind:"general",msg:e||"Number must be an integer"})})}multipleOf(e,r){return this.addCheck(n=>{if(n%e!==0)return o(n,{kind:"general",msg:r||`Number must be a multiple of ${e}`})})}},I=class extends a{validateInput(e){return a.validatePrimitive(e,"bigint",this.msg)}},F=class extends a{validateInput(e){return a.validatePrimitive(e,"boolean",this.msg)}},N=class extends a{validateInput(e){return a.validatePrimitive(e,"object",this.msg).andThen(r=>{if(r instanceof Date)return u(r);let n=r?.constructor?.name??"unknown";return i(o(e,{kind:"typeMismatch",expected:"Date instance",received:n,msg:this.msg}))})}},M=class extends a{validateInput(e){return a.validatePrimitive(e,"symbol",this.msg)}},B=class extends a{validateInput(e){return a.validatePrimitive(e,"undefined",this.msg)}},j=class extends a{validateInput(e){if(e===null)return u(e);let r=typeof e=="object"?e?.constructor?.name??"unknown":typeof e;return i(o(e,{kind:"typeMismatch",expected:"null",received:r,msg:this.msg}))}},D=class extends a{validateInput(e){if(e==null)return u();let r=typeof e=="object"?e?.constructor?.name??"unknown":typeof e;return i(o(e,{kind:"typeMismatch",expected:"void (undefined/null)",received:r,msg:this.msg}))}},$=class extends a{validateInput(e){return u(e)}},C=class extends a{validateInput(e){return u(e)}},J=class extends a{validateInput(e){return i(o(e,{kind:"typeMismatch",expected:"never",received:typeof e,msg:this.msg}))}},q=class extends a{constructor(r,n){let s,l;typeof n=="string"?s=n:typeof n=="object"&&(l=n);super(s);this.shape=r;this.objectMsg=l}strictMode=!1;objectMsg;validateInput(r){return a.validatePrimitive(r,"object",this.msg||this.objectMsg?.mismatch).andThen(n=>{if(n===null)return i(o(r,{kind:"typeMismatch",expected:"Non-null object",received:"null",msg:this.msg||this.objectMsg?.nullObject}));let s={},l=new Set(Object.keys(this.shape));for(let m of Object.keys(n)){let d=this.shape[m];if(d===void 0){if(this.strictMode){let Z=new Set(Object.keys(n)).difference(new Set(Object.keys(this.shape))).keys().toArray();return i(o(r,{kind:"unexpectedProperties",keys:Z,msg:this.msg||this.objectMsg?.unexpectedProperty}))}continue}let w=d.parse(n[m]);if(w.isErr())return i(o(r,{kind:"propertyValidation",property:m,detail:w.error.detail,msg:this.msg||this.objectMsg?.propertyValidation}));l.delete(m),s[m]=w.value}let p=l.keys().filter(m=>!a.isNullishSchema(this.shape[m])).toArray();return p.length>0?i(o(r,{kind:"missingProperties",keys:p,msg:this.msg||this.objectMsg?.missingProperty})):u(s)})}strict(){return this.strictMode=!0,this}},L=class t extends a{constructor(r,n){let s,l;typeof n=="string"?s=n:typeof n=="object"&&(l=n);super(s);this.schemas=r;this.unionMsg=l}unionMsg;static getTypeFromSchemaName(r){switch(r){case"StringSchema":case"LiteralSchema":return"string";case"NumberSchema":return"number";case"BigintSchema":return"bigint";case"BooleanSchema":return"boolean";case"UndefinedSchema":return"undefined";case"SymbolSchema":return"symbol";default:return"object"}}validateInput(r){let n=[],s=!0;for(let l of this.schemas){let p=l.parse(r);if(p.isOk())return u(p.value);s=p.error.detail?.kind==="typeMismatch"&&s,n.push(p.error.detail)}return s?i(o(r,{kind:"typeMismatch",expected:this.schemas.map(l=>t.getTypeFromSchemaName(l.constructor.name)).join(" | "),received:typeof r,msg:this.msg||this.unionMsg?.mismatch})):i(o(r,{kind:"unionValidation",msg:this.msg||this.unionMsg?.unionValidation||"Input did not match any union member",details:n}))}},K=class extends a{constructor(r,n){let s,l;typeof n=="string"?s=n:typeof n=="object"&&(l=n);super(s);this.schema=r;this.arrayMsg=l}arrayMsg;validateInput(r){if(!Array.isArray(r))return i(o(r,{kind:"typeMismatch",expected:"Array",received:"Non-array",msg:this.msg||this.arrayMsg?.mismatch}));for(let n=0;n{if("tag"in n)switch(n.tag){case"ok":return"value"in n?this.okSchema.parse(n.value).match(s=>u(u(s)),s=>i(o(r,{kind:"propertyValidation",property:"value",detail:s.detail}))):a.isNullishSchema(this.okSchema)?u(u()):i(o(r,{kind:"missingProperties",keys:["value"],msg:"If tag is set to 'ok', than result must contain a 'value' property"}));case"err":return"error"in n?this.errSchema.parse(n.error).match(s=>u(i(s)),s=>i(o(r,{kind:"propertyValidation",property:"error",detail:s.detail}))):a.isNullishSchema(this.errSchema)?u(i()):i(o(r,{kind:"missingProperties",keys:["error"],msg:"If tag is set to 'err', than result must contain a 'error' property"}));default:return i(o(r,{kind:"propertyValidation",property:"tag",detail:{kind:"typeMismatch",expected:"'ok' or 'err'",received:`'${n.tag}'`}}))}else return i(o(r,{kind:"missingProperties",keys:["tag"],msg:"Result must contain a tag property"}))})}},Q=class extends a{constructor(r){super();this.schema=r}validateInput(r){return a.validatePrimitive(r,"object").andThen(n=>{if("tag"in n)switch(n.tag){case"some":return"value"in n?this.schema.parse(n.value).match(s=>u(h(s)),s=>i(o(r,{kind:"propertyValidation",property:"value",detail:s.detail}))):a.isNullishSchema(this.schema)?u(h()):i(o(r,{kind:"missingProperties",keys:["value"],msg:"If tag is set to 'some', than option must contain a 'value' property"}));case"none":return u(y);default:return i(o(r,{kind:"propertyValidation",property:"tag",detail:{kind:"typeMismatch",expected:"'some' or 'none'",received:`'${n.tag}'`}}))}else return i(o(r,{kind:"missingProperties",keys:["tag"],msg:"Option must contain a tag property"}))})}},R={string:t=>new A(t),literal:(t,e)=>new P(t,e),number:t=>new V(t),bigint:t=>new I(t),boolean:t=>new F(t),date:t=>new N(t),symbol:t=>new M(t),undefined:t=>new B(t),null:t=>new j(t),void:t=>new D(t),any:t=>new $(t),unknown:t=>new C(t),never:t=>new J(t),obj:(t,e)=>new q(t,e),union:(t,e)=>new L(t,e),array:(t,e)=>new K(t,e),optional:(t,e)=>new z(t,e),nullable:(t,e)=>new W(t,e),nullish:(t,e)=>new G(t,e),result:(t,e)=>new H(t,e),option:t=>new Q(t)};var x=class extends U{type="RequestValidationError";constructor(e,r){super(e,r)}},S=class extends U{type="ResponseValidationError";constructor(e,r){super(e,r)}};var b=class{constructor(e,r,n){this.path=e;this.method=r;this.schema=n;this.pathSplitted=e.split("/"),this.paramIndexes=this.pathSplitted.reduce((s,l,p)=>(l.startsWith(":")&&(s[l.slice(1)]=p),s),{})}pathSplitted;paramIndexes;makeRequest(e,r){return this.schema.req.parse(e).toAsync().mapErr(n=>new x(n.input,n.detail)).andThenAsync(async n=>{let s=this.pathSplitted;for(let[d,w]of Object.entries(r))s[this.paramIndexes[d]]=w;let l=s.join("/"),m=await(await fetch(l,{method:this.method,headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})).json();return this.schema.res.parse(m).toAsync().map(d=>d).mapErr(d=>new S(d.input,d.detail))})}};var _={req:R.obj({password:R.string()}),res:R.result(R.string(),R.any())},Be=new b("/login","POST",_);export{a as BaseSchema,f as Err,T as None,c as Ok,Q as OptionSchema,v as ResultAsync,pe as ResultFromJSON,H as ResultSchema,U as SchemaValidationError,E as Some,i as err,g as errAsync,Y as flattenResult,Te as fromNullableVal,ce as fromThrowable,X as getMessageFromError,Be as loginApi,y as none,u as ok,O as okAsync,h as some,R as z};