{"version":3,"file":"custom-field.mjs","names":["formatter"],"sources":["../../../../../../src/libs/collection/custom-fields/fields/checkbox/custom-field.ts"],"sourcesContent":["import z from \"zod\";\nimport type { ServiceResponse } from \"../../../../../types.js\";\nimport deepMerge from \"../../../../../utils/helpers/deep-merge.js\";\nimport type { BooleanInt } from \"../../../../db/types.js\";\nimport formatter from \"../../../../formatters/index.js\";\nimport { copy } from \"../../../../i18n/index.js\";\nimport CustomField from \"../../custom-field.js\";\nimport type {\n\tCFConfig,\n\tCFProps,\n\tCFResponse,\n\tGetSchemaDefinitionProps,\n\tSchemaDefinition,\n} from \"../../types.js\";\nimport keyToTitle from \"../../utils/key-to-title.js\";\nimport zodSafeParse from \"../../utils/zod-safe-parse.js\";\nimport { checkboxFieldConfig } from \"./config.js\";\n\nclass CheckboxCustomField extends CustomField<\"checkbox\"> {\n\ttype = checkboxFieldConfig.type;\n\tconfig;\n\tkey;\n\tprops;\n\tconstructor(key: string, props?: CFProps<\"checkbox\">) {\n\t\tsuper();\n\t\tthis.key = key;\n\t\tthis.props = props;\n\t\tthis.config = {\n\t\t\tkey: this.key,\n\t\t\ttype: this.type,\n\t\t\tdetails: {\n\t\t\t\tlabel:\n\t\t\t\t\tthis.props?.details?.label ??\n\t\t\t\t\tcopy(`admin:fields.${this.type}.${this.key}.label`, {\n\t\t\t\t\t\tdefaultMessage: keyToTitle(this.key),\n\t\t\t\t\t}),\n\t\t\t\tsummary: this.props?.details?.summary,\n\t\t\t\ttrue: this.props?.details?.true,\n\t\t\t\tfalse: this.props?.details?.false,\n\t\t\t},\n\t\t\tlocalized: this.props?.localized ?? false,\n\t\t\tdefault: this.props?.default ?? false,\n\t\t\tindex: this.props?.index,\n\t\t\tui: {\n\t\t\t\thidden: this.props?.ui?.hidden,\n\t\t\t\tdisabled: this.props?.ui?.disabled,\n\t\t\t\tcondition: this.props?.ui?.condition,\n\t\t\t\twidth: this.props?.ui?.width,\n\t\t\t},\n\t\t\tvalidation: this.props?.validation,\n\t\t} satisfies CFConfig<\"checkbox\">;\n\t}\n\tget errors() {\n\t\treturn deepMerge(super.errors, {\n\t\t\trequired: {\n\t\t\t\tcondition: (value: unknown) =>\n\t\t\t\t\tvalue === undefined || value === null || value === 0,\n\t\t\t\tmessage: copy(\"server:core.fields.checkbox.validation.required\"),\n\t\t\t},\n\t\t});\n\t}\n\tgetSchemaDefinition(\n\t\tprops: GetSchemaDefinitionProps,\n\t): Awaited<ServiceResponse<SchemaDefinition>> {\n\t\treturn {\n\t\t\tdata: {\n\t\t\t\tcolumns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: this.key,\n\t\t\t\t\t\ttype: props.db.getDataType(\"boolean\"),\n\t\t\t\t\t\tnullable: true,\n\t\t\t\t\t\tdefault: props.db.formatInsertValue<BooleanInt>(\n\t\t\t\t\t\t\t\"boolean\",\n\t\t\t\t\t\t\tthis.config.default,\n\t\t\t\t\t\t),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\terror: undefined,\n\t\t};\n\t}\n\tformatResponseValue(value?: BooleanInt | null) {\n\t\treturn formatter.formatBoolean(\n\t\t\tBoolean(value) ?? this.config.default,\n\t\t) satisfies CFResponse<\"checkbox\">[\"value\"];\n\t}\n\tuniqueValidation(value: unknown) {\n\t\tconst valueSchema = z.union([z.literal(1), z.literal(0), z.boolean()]);\n\n\t\tconst valueValidate = zodSafeParse(value, valueSchema);\n\t\tif (!valueValidate.valid) return valueValidate;\n\n\t\treturn {\n\t\t\tvalid: true,\n\t\t};\n\t}\n}\n\nexport default CheckboxCustomField;\n"],"mappings":"kWAkBA,IAAM,EAAN,cAAkC,CAAwB,CACzD,KAAO,EAAoB,KAC3B,OACA,IACA,MACA,YAAY,EAAa,EAA6B,CACrD,MAAM,EACN,KAAK,IAAM,EACX,KAAK,MAAQ,EACb,KAAK,OAAS,CACb,IAAK,KAAK,IACV,KAAM,KAAK,KACX,QAAS,CACR,MACC,KAAK,OAAO,SAAS,OACrB,EAAK,gBAAgB,KA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