import { h, r as registerInstance } from './index-CV0Pyo1O.js';
import { S as Skeleton } from './skeleton-BEHo2ypu.js';
import { w as waitForConfig, c as configState } from './config-state-kKn89dp1.js';
import { e as generateTabId } from './utils-C25uIOK0.js';
import { c as checkPkgVersion } from './check-pkg-version-D5BxTNyL.js';
import { c as checkoutStore } from './checkout.store-DibfdUMm.js';
import './parts-CPKMvyV_.js';
import './index-cDVGjRkU.js';
import './dinero-HXpYMWUU.js';
import './package-CqcLgdsl.js';
import './index-DkoufM1t.js';

const BankAccountFormSkeleton = (props) => {
    const { isReady } = props;
    if (isReady) {
        return null;
    }
    return (h("div", { class: "container-fluid p-0" }, h("div", { class: "row mb-3" }, h("div", { class: "col-12 align-content-end" }, h(Skeleton, { height: "18px", width: "120px" }), h(Skeleton, { height: "36px" }))), h("div", { class: "row" }, h("div", { class: "col-12 align-content-end" }, h(Skeleton, { height: "18px", width: "110px" }), h(Skeleton, { height: "36px" })))));
};

const BankAccountForm = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.isReady = false;
        this.hasLoggedAchDisabledWarning = false;
    }
    async componentWillLoad() {
        await waitForConfig();
        this.iframeOrigin = configState.iframeOrigin;
        this.tabId = generateTabId();
        checkPkgVersion();
    }
    componentDidRender() {
        if (this.isAchDisabledForCheckout()) {
            return;
        }
        const elements = [
            this.accountNumberIframeElement,
            this.routingNumberIframeElement,
        ];
        if (!elements.every(Boolean)) {
            return;
        }
        Promise.all(elements.map((element) => {
            return new Promise((resolve) => {
                element.addEventListener('iframeLoaded', () => { resolve(); });
            });
        })).then(() => {
            this.isReady = true;
        });
    }
    isAchDisabledForCheckout() {
        return (checkoutStore.checkoutLoaded && !checkoutStore.achPaymentsEnabled);
    }
    async validate() {
        if (this.isAchDisabledForCheckout()) {
            return false;
        }
        const accountNumberIsValid = await this.accountNumberIframeElement.validate();
        const routingNumberIsValid = await this.routingNumberIframeElement.validate();
        return accountNumberIsValid && routingNumberIsValid;
    }
    async tokenize({ clientId, paymentMethodMetadata, account }) {
        if (this.isAchDisabledForCheckout()) {
            return {
                error: {
                    message: 'ACH payments are disabled for this checkout (payment_settings.ach_payments=false).',
                },
            };
        }
        const result = await this.accountNumberIframeElement.tokenize(clientId, paymentMethodMetadata, account);
        return result;
    }
    render() {
        if (this.isAchDisabledForCheckout()) {
            if (!this.hasLoggedAchDisabledWarning) {
                console.warn('[bank-account-form] ACH payments are disabled for this checkout (payment_settings.ach_payments=false).');
                this.hasLoggedAchDisabledWarning = true;
            }
            return null;
        }
        return (h("div", null, h(BankAccountFormSkeleton, { isReady: this.isReady }), h("hidden-input", null), h("div", { class: "container-fluid p-0", style: {
                opacity: this.isReady ? '1' : '0',
                height: this.isReady ? 'auto' : '0',
            } }, h("div", { class: "row mb-3" }, h("iframe-input", { inputId: "accountNumber", ref: (el) => (this.accountNumberIframeElement = el), label: "Account Number", iframeOrigin: `${this.iframeOrigin}/v2/accountNumber?tabId=${this.tabId}` })), h("div", { class: "row" }, h("iframe-input", { inputId: "routingNumber", ref: (el) => (this.routingNumberIframeElement = el), label: "Routing Number", iframeOrigin: `${this.iframeOrigin}/v2/routingNumber?tabId=${this.tabId}` })))));
    }
};

export { BankAccountForm as bank_account_form };
