"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.List = void 0;
const zod_1 = require("zod");
/**
 * Builder class for defining list-type form fields.
 *
 * A list presents an interface to add, remove, and reorder items. Items can be
 * either text strings ({@link List.text}) or structured objects ({@link List.obj}).
 *
 * Used with {@link Value.list} to include a list field in an {@link InputSpec}.
 */
class List {
    constructor(build, validator) {
        this.build = build;
        this.validator = validator;
        this._TYPE = null;
    }
    /**
     * Creates a list of text input items.
     *
     * @param a - List-level options (name, description, min/max length, defaults)
     * @param aSpec - Item-level options (patterns, input mode, masking, generation)
     */
    static text(a, aSpec) {
        const validator = zod_1.z.array(zod_1.z.string());
        return new List(() => {
            const spec = {
                type: 'text',
                placeholder: null,
                minLength: null,
                maxLength: null,
                masked: false,
                inputmode: 'text',
                generate: null,
                patterns: aSpec.patterns || [],
                ...aSpec,
            };
            const built = {
                description: null,
                warning: null,
                default: [],
                type: 'list',
                minLength: null,
                maxLength: null,
                disabled: false,
                ...a,
                spec,
            };
            return { spec: built, validator };
        }, validator);
    }
    /** Like {@link List.text} but options are resolved lazily at runtime via a builder function. */
    static dynamicText(getA) {
        const validator = zod_1.z.array(zod_1.z.string());
        return new List(async (options) => {
            const { spec: aSpec, ...a } = await getA(options);
            const spec = {
                type: 'text',
                placeholder: null,
                minLength: null,
                maxLength: null,
                masked: false,
                inputmode: 'text',
                generate: null,
                patterns: aSpec.patterns || [],
                ...aSpec,
            };
            const built = {
                description: null,
                warning: null,
                default: [],
                type: 'list',
                minLength: null,
                maxLength: null,
                disabled: false,
                ...a,
                spec,
            };
            return { spec: built, validator };
        }, validator);
    }
    /**
     * Creates a list of structured object items, each defined by a nested {@link InputSpec}.
     *
     * @param a - List-level options (name, description, min/max length)
     * @param aSpec - Item-level options (the nested spec, display expression, uniqueness constraint)
     */
    static obj(a, aSpec) {
        return new List(async (options) => {
            const { spec: previousSpecSpec, ...restSpec } = aSpec;
            const built = await previousSpecSpec.build(options);
            const spec = {
                type: 'object',
                displayAs: null,
                uniqueBy: null,
                ...restSpec,
                spec: built.spec,
            };
            const value = {
                spec,
                default: [],
                ...a,
            };
            return {
                spec: {
                    description: null,
                    warning: null,
                    minLength: null,
                    maxLength: null,
                    type: 'list',
                    disabled: false,
                    ...value,
                },
                validator: zod_1.z.array(built.validator),
            };
        }, zod_1.z.array(aSpec.spec.validator));
    }
}
exports.List = List;
//# sourceMappingURL=list.js.map