{"version":3,"file":"custom-field.mjs","names":[],"sources":["../../../../../../src/libs/collection/custom-fields/fields/range/custom-field.ts"],"sourcesContent":["import z from \"zod\";\nimport type { ServiceResponse } from \"../../../../../types.js\";\nimport { copy } from \"../../../../i18n/index.js\";\nimport prefixGeneratedColName from \"../../../helpers/prefix-generated-column-name.js\";\nimport CustomField from \"../../custom-field.js\";\nimport type {\n\tCFConfig,\n\tCFProps,\n\tCFResponse,\n\tCustomFieldErrorItem,\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 { rangeFieldConfig } from \"./config.js\";\nimport {\n\tnormalizeRangeInputValue,\n\tnormalizeStoredRangeValues,\n} from \"./values.js\";\n\nconst DEFAULT_MIN = 0;\nconst DEFAULT_MAX = 100;\nconst DEFAULT_STEP = 1;\n\nclass RangeCustomField extends CustomField<\"range\"> {\n\ttype = rangeFieldConfig.type;\n\tconfig;\n\tkey;\n\tprops;\n\tconstructor(key: string, props?: CFProps<\"range\">) {\n\t\tsuper();\n\t\tthis.key = key;\n\t\tthis.props = props;\n\n\t\tconst min = this.props?.min ?? DEFAULT_MIN;\n\t\tconst max = this.props?.max ?? DEFAULT_MAX;\n\t\tconst step = this.props?.step ?? DEFAULT_STEP;\n\t\tconst thumbs = this.props?.thumbs;\n\t\tconst defaultValue =\n\t\t\tthis.props?.default ?? (thumbs === 2 ? [min, max] : [min]);\n\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},\n\t\t\tmin,\n\t\t\tmax,\n\t\t\tstep,\n\t\t\tlocalized: this.props?.localized ?? false,\n\t\t\tdefault: normalizeStoredRangeValues(defaultValue, thumbs),\n\t\t\tindex: this.props?.index,\n\t\t\tthumbs,\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<\"range\">;\n\t}\n\toverride normalizeInputValue(value: unknown) {\n\t\treturn normalizeRangeInputValue(value, this.config.thumbs);\n\t}\n\toverride get defaultValue(): unknown {\n\t\treturn normalizeStoredRangeValues(this.config.default, this.config.thumbs);\n\t}\n\toverride get errors(): {\n\t\tfieldType: CustomFieldErrorItem;\n\t\trequired: CustomFieldErrorItem;\n\t\tzod: CustomFieldErrorItem;\n\t} {\n\t\treturn {\n\t\t\t...super.errors,\n\t\t\trequired: {\n\t\t\t\tcondition: (value: unknown) =>\n\t\t\t\t\tvalue === undefined ||\n\t\t\t\t\tvalue === null ||\n\t\t\t\t\t(Array.isArray(value) && value.length === 0),\n\t\t\t\tmessage: copy(\"server:core.fields.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: \"value\",\n\t\t\t\t\t\ttype: props.db.getDataType(\"real\"),\n\t\t\t\t\t\tnullable: false,\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: unknown) {\n\t\tconst normalized = normalizeStoredRangeValues(value, this.config.thumbs);\n\t\treturn (\n\t\t\tnormalized.length > 0\n\t\t\t\t? normalized\n\t\t\t\t: normalizeStoredRangeValues(this.config.default, this.config.thumbs)\n\t\t) satisfies CFResponse<\"range\">[\"value\"];\n\t}\n\toverride get relationValueColumn() {\n\t\treturn \"value\";\n\t}\n\toverride serializeRelationFieldValue(\n\t\tvalue: unknown,\n\t): Array<Record<string, unknown>> {\n\t\treturn normalizeStoredRangeValues(value, this.config.thumbs).map(\n\t\t\t(rangeValue) => ({\n\t\t\t\t[prefixGeneratedColName(\"value\")]: rangeValue,\n\t\t\t}),\n\t\t);\n\t}\n\tuniqueValidation(value: unknown) {\n\t\tconst expectedLength = this.config.thumbs === 2 ? 2 : 1;\n\t\tconst valueSchema = z\n\t\t\t.array(z.number().min(this.config.min).max(this.config.max))\n\t\t\t.refine(\n\t\t\t\t(values) => values.length === 0 || values.length === expectedLength,\n\t\t\t\t{\n\t\t\t\t\tmessage: `Expected ${expectedLength} range value${expectedLength === 1 ? \"\" : \"s\"}`,\n\t\t\t\t},\n\t\t\t)\n\t\t\t.refine(\n\t\t\t\t(values) =>\n\t\t\t\t\tvalues.every((item) => {\n\t\t\t\t\t\tconst steps = (item - this.config.min) / this.config.step;\n\t\t\t\t\t\treturn Math.abs(steps - Math.round(steps)) < 1e-9;\n\t\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\tmessage: `Range values must align to a step of ${this.config.step}`,\n\t\t\t\t},\n\t\t\t);\n\n\t\treturn zodSafeParse(value, valueSchema);\n\t}\n}\n\nexport default RangeCustomField;\n"],"mappings":"6YAyBA,IAAM,EAAN,cAA+B,CAAqB,CACnD,KAAO,EAAiB,KACxB,OACA,IACA,MACA,YAAY,EAAa,EAA0B,CAClD,MAAM,EACN,KAAK,IAAM,EACX,KAAK,MAAQ,EAEb,IAAM,EAAM,KAAK,OAAO,KAAO,EACzB,EAAM,KAAK,OAAO,KAAO,IACzB,EAAO,KAAK,OAAO,MAAQ,EAC3B,EAAS,KAAK,OAAO,OACrB,EACL,KAAK,OAAO,UAAY,IAAW,EAAI,CAAC,EAAK,CAAG,EAAI,CAAC,CAAG,GAEzD,KAAK,OA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