'use strict';

var index = require('./index-p7cvUYAE.js');
var skeleton = require('./skeleton-DvRCiQlA.js');
var configState = require('./config-state-BIgGhhe9.js');
var utils = require('./utils-CYONRFR6.js');
var checkPkgVersion = require('./check-pkg-version-B9EoYyZR.js');
var checkout_store = require('./checkout.store-CZHrIuaP.js');
require('./parts-BXshb_S-.js');
require('./index-W-zoNiyM.js');
require('./dinero-BwNN_k7x.js');
require('./package-YyJzVqrh.js');
require('./index-CajjbB9t.js');

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

const BankAccountForm = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.isReady = false;
        this.hasLoggedAchDisabledWarning = false;
    }
    async componentWillLoad() {
        await configState.waitForConfig();
        this.iframeOrigin = configState.configState.iframeOrigin;
        this.tabId = utils.generateTabId();
        checkPkgVersion.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 (checkout_store.checkoutStore.checkoutLoaded && !checkout_store.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 (index.h("div", null, index.h(BankAccountFormSkeleton, { isReady: this.isReady }), index.h("hidden-input", null), index.h("div", { class: "container-fluid p-0", style: {
                opacity: this.isReady ? '1' : '0',
                height: this.isReady ? 'auto' : '0',
            } }, index.h("div", { class: "row mb-3" }, index.h("iframe-input", { inputId: "accountNumber", ref: (el) => (this.accountNumberIframeElement = el), label: "Account Number", iframeOrigin: `${this.iframeOrigin}/v2/accountNumber?tabId=${this.tabId}` })), index.h("div", { class: "row" }, index.h("iframe-input", { inputId: "routingNumber", ref: (el) => (this.routingNumberIframeElement = el), label: "Routing Number", iframeOrigin: `${this.iframeOrigin}/v2/routingNumber?tabId=${this.tabId}` })))));
    }
};

exports.bank_account_form = BankAccountForm;
