import { r as registerInstance, c as createEvent, h, g as getElement } from './index-77a9c9e7.js';
import { e as e$, f as $w } from './index.esm.min-cd800f97.js';

const bpSdkVehicleVinInputContainerCss = "";
const BpSdkVehicleVinInputContainerStyle0 = bpSdkVehicleVinInputContainerCss;

const BpSdkVehicleVinInputContainer = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.vinUpdate = createEvent(this, "vinUpdate", 7);
        this.onFocusOut = () => {
            this.touched = true;
            this.updateErrors(this.pristine, true, this.value);
        };
        this.value = undefined;
        this.answerId = undefined;
        this.code = e$(8);
        this.useExternal = false;
        this.touched = false;
        this.pristine = true;
        this.errors = [];
    }
    resetState() {
        this.touched = false;
        this.pristine = true;
        this.errors = [];
    }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-vehicle-vin-input-container');
        customElements.whenDefined('bp-sdk-external-vehicle-vin-input-container').then(() => {
            this.useExternal = true;
        });
    }
    loadFocusEvents() {
        if (!this.useExternal) {
            this.el.addEventListener('focusout', this.onFocusOut);
        }
    }
    unloadFocusEvents() {
        this.el.removeEventListener('focusout', this.onFocusOut);
    }
    componentDidLoad() {
        this.loadFocusEvents();
    }
    disconnectedCallback() {
        this.unloadFocusEvents();
    }
    onValueUpdate(evt) {
        var _a;
        if (this.useExternal) {
            return;
        }
        this.pristine = false;
        this.value = ((_a = evt.detail) !== null && _a !== void 0 ? _a : [])[0];
        this.updateErrors(false, this.touched, this.value);
        if (this.errors.length === 0) {
            this.vinUpdate.emit({ vin: this.value, answerId: this.answerId });
        }
    }
    updateErrors(pristine, touched, value) {
        this.errors = $w(pristine, touched, value) ? ['required'] : [];
    }
    onMakeDirty() {
        this.touched = true;
        this.pristine = false;
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return h("bp-sdk-external-vehicle-vin-input-container", null);
    }
    renderInternal() {
        return (h("bp-sdk-vehicle-vin-input", { value: this.value, code: this.code, label: "Enter vehicle VIN", touched: this.touched, pristine: this.pristine, errors: this.errors }));
    }
    get el() { return getElement(this); }
};
BpSdkVehicleVinInputContainer.style = BpSdkVehicleVinInputContainerStyle0;

export { BpSdkVehicleVinInputContainer as bp_sdk_vehicle_vin_input_container };
