import vine, { BaseLiteralType, Vine, symbols } from '@vinejs/vine';
import camelcase from 'camelcase';
Vine.macro('instanceof', function (expectedClass, options) {
    return new VineInstanceOf(expectedClass, options?.schema, options);
});
export class VineInstanceOf extends BaseLiteralType {
    [symbols.SUBTYPE] = 'instanceof';
    expectedClass;
    schema;
    validator;
    allowUntyped;
    constructor(expectedClass, schema, options, validations) {
        const allowUntyped = options?.allowUntyped === undefined ? true : options?.allowUntyped;
        super(options, validations || allowUntyped ? [] : [isInstanceOf(expectedClass)()]);
        this.expectedClass = expectedClass;
        this.allowUntyped = allowUntyped;
        if (schema)
            this.validator = vine.compile(schema);
    }
    clone() {
        return new VineInstanceOf(this.expectedClass, this.schema, {
            ...this.cloneOptions(),
            allowUntyped: this.allowUntyped,
        }, this.cloneValidations());
    }
    [symbols.PARSE](propertyName, refs, options) {
        return {
            type: 'literal',
            subtype: this[symbols.SUBTYPE],
            fieldName: propertyName,
            propertyName: options.toCamelCase ? camelcase(propertyName) : propertyName,
            bail: this.options.bail,
            allowNull: this.options.allowNull,
            isOptional: this.options.isOptional,
            parseFnId: this.options.parse ? refs.trackParser(this.options.parse) : undefined,
            validations: this.compileValidations(refs),
            expectedClass: this.expectedClass.name,
            schema: this.validator?.toJSON().schema.schema,
            allowUntyped: this.allowUntyped,
        };
    }
}
const isInstanceOf = (expectedClass) => vine.createRule((value, _, field) => {
    if (!(value instanceof expectedClass)) {
        field.report(`The {{ field }} field class must be an ${expectedClass}`, 'instanceof', field);
        return;
    }
});
//# sourceMappingURL=vine_instanceof.js.map