import { deepEqual } from "../../../utils/compare-utils.js";
class ValidatePropsController {
    hostConnected() {
        if (null === this.host.error) this.host.error = void 0;
    }
    hostUpdate() {
        if (this.host.error && this.host.error !== this.lastValidationError) return;
        this.currentProps = this.getProps();
        if (deepEqual(this.currentProps, this.previousProps)) return;
        this.host.error = void 0;
        this.validateProps();
    }
    validateProps() {
        try {
            this.schema.validate(this.currentProps);
        } catch (error) {
            if (this.throwOnError) {
                this.host.error = error;
                this.lastValidationError = error;
            } else {
                const message = `Prop validation failed for component ${this.host.tagName?.toLowerCase()}: ${error.message}`;
                console.warn(message, this.host);
            }
        } finally{
            this.previousProps = this.currentProps;
        }
    }
    constructor(host, getProps, schema, throwOnError = true){
        this.host = host;
        this.getProps = getProps;
        this.schema = schema;
        this.throwOnError = throwOnError;
        host.addController(this);
    }
}
export { ValidatePropsController };
