import { r as registerInstance, h, H as Host } from './index-CV0Pyo1O.js';
import { c as create$3, F as FormController } from './index.esm-KCSohgHx.js';
import { y as postalValidation, z as stateValidation, h as cityValidation, l as lineTwoValidation, j as lineOneValidation, v as identityNameValidation, A as checkoutBillingStateOptions } from './schema-validations-CHxw3FkH.js';
import { al as billingForm } from './parts-CPKMvyV_.js';
import { c as checkoutStore, o as onChange } from './checkout.store-DibfdUMm.js';
import { P as PAYMENT_METHODS } from './index-DkoufM1t.js';
import { H as Header3 } from './header-3-BUDap2xY.js';
import './event-types-CZfYo5-1.js';
import './_commonjsHelpers-BFTU3MAI.js';
import './country-config-CEJVOOGe.js';
import './Business-BcNb9bVO.js';
import './business-form-options-DXjZp4Rl.js';
import './index-cDVGjRkU.js';

const fullBillingSchema = () => create$3({
    name: identityNameValidation.required('Enter full name'),
    address_line1: lineOneValidation.required('Enter street address'),
    address_line2: lineTwoValidation.nullable(),
    address_city: cityValidation.required('Enter city'),
    address_state: stateValidation.required('Select state or province'),
    address_postal_code: postalValidation.required('Enter postal code'),
});
const postalOnlySchema = () => create$3({
    name: identityNameValidation.nullable(),
    address_line1: lineOneValidation.nullable(),
    address_line2: lineTwoValidation.nullable(),
    address_city: cityValidation.nullable(),
    address_state: stateValidation.nullable(),
    address_postal_code: postalValidation.required('Enter postal code'),
});
const nameOnlySchema = () => create$3({
    name: identityNameValidation.required('Enter full name'),
    address_line1: lineOneValidation.nullable(),
    address_line2: lineTwoValidation.nullable(),
    address_city: cityValidation.nullable(),
    address_state: stateValidation.nullable(),
    address_postal_code: postalValidation.nullable(),
});

const BankAccountBillingFormSimple = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        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 })))))));
    }
};

const BillingForm = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
    }
    get showSimpleCardBillingForm() {
        return this.paymentMethodType === PAYMENT_METHODS.NEW_CARD && this.hideCardBillingForm;
    }
    get showSimpleBankAccountBillingForm() {
        return this.paymentMethodType === PAYMENT_METHODS.NEW_BANK_ACCOUNT && this.hideBankAccountBillingForm;
    }
    async getValues() {
        var _a;
        return (_a = this.selectedFormRef) === null || _a === void 0 ? void 0 : _a.getValues();
    }
    async validate() {
        var _a;
        return (_a = this.selectedFormRef) === null || _a === void 0 ? void 0 : _a.validate();
    }
    render() {
        if (this.showSimpleBankAccountBillingForm) {
            return (h(Host, null, h("bank-account-billing-form-simple", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
        }
        if (this.showSimpleCardBillingForm) {
            return (h(Host, null, h("card-billing-form-simple", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
        }
        return (h(Host, null, h("billing-form-full", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
    }
};

const BillingFormFull = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.errors = {};
        this.inputHandler = (name, value) => {
            this.formController.setValues(Object.assign(Object.assign({}, this.formController.values.getValue()), { [name]: value }));
        };
    }
    componentWillLoad() {
        this.formController = new FormController(fullBillingSchema());
        const fields = checkoutStore.billingFormFields;
        if (fields && Object.keys(fields).some((k) => fields[k])) {
            this.formController.setInitialValues(fields);
        }
        this.formController.values.subscribe(values => this.billingInfo = Object.assign({}, values));
        this.formController.errors.subscribe(errors => {
            this.errors = Object.assign({}, errors);
        });
    }
    componentDidLoad() {
        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() {
        if (!this.formController) {
            return null;
        }
        const billingFormDefaultValue = this.formController.getInitialValues();
        return (h("div", { part: billingForm, class: "mt-4" }, h(Header3, { text: "Billing address", class: "fs-6 fw-bold lh-lg mb-4" }), h("form", null, h("fieldset", null, this.legend && (h("legend", null, this.legend)), h("div", { class: "row gy-3" }, h("div", { class: "col-12" }, h("form-control-text", { name: 'name', label: 'Full Name', defaultValue: billingFormDefaultValue.name, errorText: this.errors.name, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_line1', label: 'Street Address', defaultValue: billingFormDefaultValue.address_line1, errorText: this.errors.address_line1, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_line2', label: "Apartment, Suite, etc. (optional)", defaultValue: billingFormDefaultValue.address_line2, errorText: this.errors.address_line2, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_city', label: "City", defaultValue: billingFormDefaultValue.address_city, errorText: this.errors.address_city, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-select", { name: 'address_state', label: 'State / Province', options: checkoutBillingStateOptions, defaultValue: billingFormDefaultValue.address_state, errorText: this.errors.address_state, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_postal_code', label: "Postal Code", defaultValue: billingFormDefaultValue.address_postal_code, errorText: this.errors.address_postal_code, inputHandler: this.inputHandler, maxLength: 7 })))))));
    }
};

const CardBillingFormSimple = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.errors = {};
        this.inputHandler = (name, value) => {
            this.formController.setValues(Object.assign(Object.assign({}, this.formController.values.getValue()), { [name]: value }));
        };
    }
    componentWillLoad() {
        this.formController = new FormController(postalOnlySchema());
        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: '41a7f26e12bfa3da4e7926d8a535218a0580be14', part: billingForm }, h("form", { key: 'b48cf148ec6d16fd8e5859dfe96a3b9fd6d3036d' }, h("fieldset", { key: '88094544642af8fadb270dfafd838d450ad88cdd' }, this.legend && (h("legend", { key: '13c19557aa4c0c127305cc54f6346eeb74d4f750' }, this.legend)), h("div", { key: 'bbcf1a622720df2d21db718eaf823e4e33155070', class: "row gy-3" }, h("div", { key: '96b9d590fd68fd4f2a54760e58c0f2bc166a832b', class: "col-12" }, h("form-control-text", { key: '57050d0274782cb2ff5f1a9b13726a8319438a36', name: 'address_postal_code', label: "Postal Code", defaultValue: billingFormDefaultValue.address_postal_code, errorText: this.errors.address_postal_code, inputHandler: this.inputHandler, maxLength: 7 })))))));
    }
};

export { BankAccountBillingFormSimple as bank_account_billing_form_simple, BillingForm as billing_form, BillingFormFull as billing_form_full, CardBillingFormSimple as card_billing_form_simple };
