'use strict';

var index = require('./index-p7cvUYAE.js');
var index_esm = require('./index.esm-BNETBIV_.js');
var schemaValidations = require('./schema-validations-Cs0mSKR7.js');
var parts = require('./parts-BXshb_S-.js');
var checkout_store = require('./checkout.store-CZHrIuaP.js');
var index$1 = require('./index-CajjbB9t.js');
var header3 = require('./header-3-DCYJhPro.js');
require('./event-types-YOY5TKUN.js');
require('./_commonjsHelpers-BJu3ubxk.js');
require('./country-config-C1bWmF8P.js');
require('./Business-oBifyXkW.js');
require('./business-form-options-CnlrfbQ6.js');
require('./index-W-zoNiyM.js');

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

const BankAccountBillingFormSimple = class {
    constructor(hostRef) {
        index.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 index_esm.FormController(nameOnlySchema());
        const fields = checkout_store.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 = checkout_store.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 (index.h("div", { key: 'e8c53c98793d0296d69799c8449d762084d9859b', part: parts.billingForm, class: "mt-4" }, index.h("form", { key: '83e59bb7bdcf4a3e0b4ed4320358796eae91726f' }, index.h("fieldset", { key: 'd92a4a02c531a08a270562701bc71440add5b545' }, this.legend && (index.h("legend", { key: '74ac1eb2c6fc629323f31be515e3aa204ae302a2' }, this.legend)), index.h("div", { key: '161888eb0680a4c9d6280440961f2926b08ac1ed', class: "row gy-3" }, index.h("div", { key: '9f6c899b7a4ff25a5eba5278bffe7d4e86eb00df', class: "col-12" }, index.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) {
        index.registerInstance(this, hostRef);
    }
    get showSimpleCardBillingForm() {
        return this.paymentMethodType === index$1.PAYMENT_METHODS.NEW_CARD && this.hideCardBillingForm;
    }
    get showSimpleBankAccountBillingForm() {
        return this.paymentMethodType === index$1.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 (index.h(index.Host, null, index.h("bank-account-billing-form-simple", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
        }
        if (this.showSimpleCardBillingForm) {
            return (index.h(index.Host, null, index.h("card-billing-form-simple", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
        }
        return (index.h(index.Host, null, index.h("billing-form-full", { legend: this.legend, ref: (el) => (this.selectedFormRef = el) })));
    }
};

const BillingFormFull = class {
    constructor(hostRef) {
        index.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 index_esm.FormController(fullBillingSchema());
        const fields = checkout_store.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 = checkout_store.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 (index.h("div", { part: parts.billingForm, class: "mt-4" }, index.h(header3.Header3, { text: "Billing address", class: "fs-6 fw-bold lh-lg mb-4" }), index.h("form", null, index.h("fieldset", null, this.legend && (index.h("legend", null, this.legend)), index.h("div", { class: "row gy-3" }, index.h("div", { class: "col-12" }, index.h("form-control-text", { name: 'name', label: 'Full Name', defaultValue: billingFormDefaultValue.name, errorText: this.errors.name, inputHandler: this.inputHandler })), index.h("div", { class: "col-12" }, index.h("form-control-text", { name: 'address_line1', label: 'Street Address', defaultValue: billingFormDefaultValue.address_line1, errorText: this.errors.address_line1, inputHandler: this.inputHandler })), index.h("div", { class: "col-12" }, index.h("form-control-text", { name: 'address_line2', label: "Apartment, Suite, etc. (optional)", defaultValue: billingFormDefaultValue.address_line2, errorText: this.errors.address_line2, inputHandler: this.inputHandler })), index.h("div", { class: "col-12" }, index.h("form-control-text", { name: 'address_city', label: "City", defaultValue: billingFormDefaultValue.address_city, errorText: this.errors.address_city, inputHandler: this.inputHandler })), index.h("div", { class: "col-12" }, index.h("form-control-select", { name: 'address_state', label: 'State / Province', options: schemaValidations.checkoutBillingStateOptions, defaultValue: billingFormDefaultValue.address_state, errorText: this.errors.address_state, inputHandler: this.inputHandler })), index.h("div", { class: "col-12" }, index.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) {
        index.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 index_esm.FormController(postalOnlySchema());
        const fields = checkout_store.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 = checkout_store.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 (index.h("div", { key: '41a7f26e12bfa3da4e7926d8a535218a0580be14', part: parts.billingForm }, index.h("form", { key: 'b48cf148ec6d16fd8e5859dfe96a3b9fd6d3036d' }, index.h("fieldset", { key: '88094544642af8fadb270dfafd838d450ad88cdd' }, this.legend && (index.h("legend", { key: '13c19557aa4c0c127305cc54f6346eeb74d4f750' }, this.legend)), index.h("div", { key: 'bbcf1a622720df2d21db718eaf823e4e33155070', class: "row gy-3" }, index.h("div", { key: '96b9d590fd68fd4f2a54760e58c0f2bc166a832b', class: "col-12" }, index.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 })))))));
    }
};

exports.bank_account_billing_form_simple = BankAccountBillingFormSimple;
exports.billing_form = BillingForm;
exports.billing_form_full = BillingFormFull;
exports.card_billing_form_simple = CardBillingFormSimple;
