'use strict';

var index = require('./index-W-zoNiyM.js');
var index$1 = require('./index-CajjbB9t.js');

const initialState = {
    accountId: '',
    authToken: '',
    billingFormFields: {
        address_postal_code: '',
    },
    bnplEnabled: false,
    isSubmitting: false,
    isWalletProcessing: false,
    applePayEnabled: false,
    googlePayEnabled: false,
    achPaymentsEnabled: false,
    insuranceEnabled: false,
    bnplProviderApiVersion: '',
    bnplProviderCheckoutUrl: '',
    bnplProviderClientId: '',
    bnplProviderMode: '',
    checkoutId: '',
    checkoutLoaded: false,
    checkoutMode: null,
    bankAccountVerification: undefined,
    disableBankAccount: false,
    disableCreditCard: false,
    disablePaymentMethodGroup: false,
    paymentAmount: 0,
    paymentCurrency: 'USD',
    paymentDescription: '',
    paymentMethodGroupId: undefined,
    paymentMethods: [],
    paymentToken: undefined,
    savePaymentMethod: false,
    selectedPaymentMethod: undefined,
    totalAmount: 0,
    plaidPublicToken: undefined,
    plaidLinkTokenId: undefined,
};
const { state: checkoutStore, onChange } = index.createStore(initialState);
Object.keys(initialState).forEach((key) => {
    // @ts-ignore
    onChange(key, (newValue) => {
        checkoutStore[key] = newValue;
    });
});
// Helper: compute available payment methods based on store flags
function getAvailablePaymentMethodTypes() {
    var _a;
    const methods = [];
    const achAllowedForCheckout = !checkoutStore.checkoutLoaded || checkoutStore.achPaymentsEnabled;
    if (!checkoutStore.disablePaymentMethodGroup &&
        ((_a = checkoutStore.paymentMethods) === null || _a === void 0 ? void 0 : _a.length)) {
        const hasSavedCard = checkoutStore.paymentMethods.some((pm) => pm.type === index$1.PAYMENT_METHODS.SAVED_CARD);
        const hasSavedBank = checkoutStore.paymentMethods.some((pm) => pm.type === index$1.PAYMENT_METHODS.SAVED_BANK_ACCOUNT);
        if (hasSavedCard && !checkoutStore.disableCreditCard) {
            methods.push(index$1.PAYMENT_METHODS.SAVED_CARD);
        }
        if (hasSavedBank &&
            !checkoutStore.disableBankAccount &&
            achAllowedForCheckout) {
            methods.push(index$1.PAYMENT_METHODS.SAVED_BANK_ACCOUNT);
        }
    }
    if (!checkoutStore.disableCreditCard) {
        methods.push(index$1.PAYMENT_METHODS.NEW_CARD);
    }
    if (!checkoutStore.disableBankAccount && achAllowedForCheckout) {
        methods.push(index$1.PAYMENT_METHODS.NEW_BANK_ACCOUNT);
    }
    if (checkoutStore.bnplEnabled) {
        methods.push(index$1.PAYMENT_METHODS.SEZZLE);
    }
    if (checkoutStore.applePayEnabled) {
        methods.push(index$1.PAYMENT_METHODS.APPLE_PAY);
    }
    if (checkoutStore.googlePayEnabled) {
        methods.push(index$1.PAYMENT_METHODS.GOOGLE_PAY);
    }
    if (checkoutStore.bankAccountVerification === true &&
        !checkoutStore.disableBankAccount &&
        achAllowedForCheckout) {
        methods.push(index$1.PAYMENT_METHODS.PLAID);
    }
    return methods;
}
function getCheckoutState() {
    var _a;
    return {
        selectedPaymentMethod: checkoutStore.selectedPaymentMethod,
        paymentAmount: checkoutStore.paymentAmount,
        totalAmount: checkoutStore.totalAmount,
        paymentCurrency: checkoutStore.paymentCurrency,
        paymentDescription: checkoutStore.paymentDescription,
        savedPaymentMethods: checkoutStore.paymentMethods,
        savePaymentMethod: checkoutStore.savePaymentMethod,
        bnplEnabled: checkoutStore.bnplEnabled,
        applePayEnabled: checkoutStore.applePayEnabled,
        insuranceEnabled: checkoutStore.insuranceEnabled,
        disableBankAccount: checkoutStore.disableBankAccount,
        disableCreditCard: checkoutStore.disableCreditCard,
        disablePaymentMethodGroup: checkoutStore.disablePaymentMethodGroup,
        paymentToken: (_a = checkoutStore.paymentToken) !== null && _a !== void 0 ? _a : undefined,
    };
}
function onAnyChange(callback) {
    Object.keys(initialState).forEach((key) => {
        // @ts-ignore - onChange is generically typed by stencil/store but not exported here
        onChange(key, (newValue) => callback(key, newValue));
    });
}

exports.checkoutStore = checkoutStore;
exports.getAvailablePaymentMethodTypes = getAvailablePaymentMethodTypes;
exports.getCheckoutState = getCheckoutState;
exports.onAnyChange = onAnyChange;
exports.onChange = onChange;
