{"version":3,"file":"refine.mjs","sources":["../../src/other-types/refine.mts"],"sourcesContent":["import { pipe, Result } from 'ts-data-forge';\nimport { type Primitive } from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n  createPrimitiveValidationError,\n  createType,\n  type ValidationError,\n  type ValidationErrorDetails,\n} from '../utils/index.mjs';\n\nexport const refine = <\n  Base extends Extract<Primitive, string | boolean | bigint | number>,\n  R extends Base,\n>({\n  baseType,\n  is,\n  defaultValue,\n  typeName = `${baseType.typeName} refined`,\n  getConstraintDetails,\n}: Readonly<{\n  baseType: Type<Base>;\n  is: (a: Base) => a is R;\n  defaultValue: R;\n  typeName?: string;\n  /**\n   * Optional producer of structured error details for a value that passes the\n   * base type but fails `is`. Used by the built-in constrained primitives\n   * (`string` / `number` / `bigint`) to explain which constraint was violated;\n   * user-defined refinements can omit it and fall back to a generic message.\n   */\n  getConstraintDetails?: (a: Base) => ValidationErrorDetails | undefined;\n}>): Type<R> => {\n  const validate: Type<R>['validate'] = (a) =>\n    pipe(a)\n      .map(baseType.validate)\n      .map((res): Result<R, readonly ValidationError[]> =>\n        Result.isErr(res)\n          ? res\n          : is(res.value)\n            ? Result.ok(res.value)\n            : Result.err([\n                createPrimitiveValidationError({\n                  actualValue: a,\n                  expectedType: typeName,\n                  typeName,\n                  details: getConstraintDetails?.(res.value),\n                }),\n              ]),\n      ).value;\n\n  return createType({\n    typeName,\n    defaultValue,\n    validate,\n  });\n};\n"],"names":[],"mappings":";;;;AAUO,MAAM,SAAS,CAGpB;AAAA,EACA,QAAA;AAAA,EACA,EAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA,GAAW,CAAA,EAAG,QAAA,CAAS,QAAQ,CAAA,QAAA,CAAA;AAAA,EAC/B;AACF,CAAA,KAYgB;AACd,EAAA,MAAM,QAAA,GAAgC,CAAC,CAAA,KACrC,IAAA,CAAK,CAAC,CAAA,CACH,GAAA,CAAI,QAAA,CAAS,QAAQ,CAAA,CACrB,GAAA;AAAA,IAAI,CAAC,GAAA,KACJ,MAAA,CAAO,KAAA,CAAM,GAAG,IACZ,GAAA,GACA,EAAA,CAAG,GAAA,CAAI,KAAK,IACV,MAAA,CAAO,EAAA,CAAG,IAAI,KAAK,CAAA,GACnB,OAAO,GAAA,CAAI;AAAA,MACT,8BAAA,CAA+B;AAAA,QAC7B,WAAA,EAAa,CAAA;AAAA,QACb,YAAA,EAAc,QAAA;AAAA,QACd,QAAA;AAAA,QACA,OAAA,EAAS,oBAAA,GAAuB,GAAA,CAAI,KAAK;AAAA,OAC1C;AAAA,KACF;AAAA,GACT,CAAE,KAAA;AAEN,EAAA,OAAO,UAAA,CAAW;AAAA,IAChB,QAAA;AAAA,IACA,YAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH;;;;"}