import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { n as nameOnlySchema } from './billing-form-schema.js';
import { x as billingForm } from './parts.js';
import { F as FormController } from './form.js';
import { c as checkoutStore, o as onChange } from './checkout.store.js';
import { d as defineCustomElement$3 } from './custom-popper2.js';
import { d as defineCustomElement$2 } from './form-control-text2.js';
import { d as defineCustomElement$1 } from './form-control-tooltip2.js';

const BankAccountBillingFormSimple = /*@__PURE__*/ proxyCustomElement(class BankAccountBillingFormSimple extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.errors = {};
        this.inputHandler = (name, value) => {
            this.formController.setValues(Object.assign(Object.assign({}, this.formController.values.getValue()), { [name]: value }));
        };
    }
    componentWillLoad() {
        this.formController = new FormController(nameOnlySchema());
        const fields = checkoutStore.billingFormFields;
        if (fields && Object.keys(fields).some((k) => fields[k])) {
            this.formController.setInitialValues(fields);
        }
    }
    componentDidLoad() {
        this.formController.values.subscribe(values => this.billingInfo = Object.assign({}, values));
        this.formController.errors.subscribe(errors => {
            this.errors = Object.assign({}, errors);
        });
        this.unsubscribeFromStore = onChange('billingFormFields', (newValue) => {
            this.formController.setInitialValues(newValue);
        });
    }
    disconnectedCallback() {
        var _a;
        (_a = this.unsubscribeFromStore) === null || _a === void 0 ? void 0 : _a.call(this);
    }
    async getValues() {
        return this.formController.values.getValue();
    }
    async validate() {
        const isValid = await this.formController.validate();
        const errors = this.formController.errors.getValue();
        return { isValid, errors };
    }
    render() {
        const billingFormDefaultValue = this.formController.getInitialValues();
        return (h("div", { key: 'e8c53c98793d0296d69799c8449d762084d9859b', part: billingForm, class: "mt-4" }, h("form", { key: '83e59bb7bdcf4a3e0b4ed4320358796eae91726f' }, h("fieldset", { key: 'd92a4a02c531a08a270562701bc71440add5b545' }, this.legend && (h("legend", { key: '74ac1eb2c6fc629323f31be515e3aa204ae302a2' }, this.legend)), h("div", { key: '161888eb0680a4c9d6280440961f2926b08ac1ed', class: "row gy-3" }, h("div", { key: '9f6c899b7a4ff25a5eba5278bffe7d4e86eb00df', class: "col-12" }, h("form-control-text", { key: '74a9958fb74de730067fe4d87dfac33c12522169', name: 'name', label: 'Full Name', defaultValue: billingFormDefaultValue.name, errorText: this.errors.name, inputHandler: this.inputHandler })))))));
    }
}, [256, "bank-account-billing-form-simple", {
        "legend": [1025],
        "formController": [32],
        "billingInfo": [32],
        "errors": [32],
        "getValues": [64],
        "validate": [64]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["bank-account-billing-form-simple", "custom-popper", "form-control-text", "form-control-tooltip"];
    components.forEach(tagName => { switch (tagName) {
        case "bank-account-billing-form-simple":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, BankAccountBillingFormSimple);
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { BankAccountBillingFormSimple as B, defineCustomElement as d };
