import { h } from "@stencil/core";
import JustifiAnalytics from "../../api/Analytics";
import { checkPkgVersion } from "../../utils/check-pkg-version";
import { checkoutStore } from "../../store/checkout.store";
import { checkoutSummary } from "../../styles/parts";
import { StyledHost } from "../../ui-components";
import { PAYMENT_METHODS } from "../modular-checkout/ModularCheckout";
export class JustifiCheckout {
    constructor() {
        this.availablePaymentMethods = [];
        this.errorMessage = '';
        this.insuranceToggled = false;
        this.disableBankAccount = false;
        this.disableBnpl = false;
        this.disableCreditCard = false;
        this.disablePaymentMethodGroup = false;
        this.hideBankAccountBillingForm = false;
        this.hideCardBillingForm = false;
    }
    propChanged() {
        this.updateStore();
    }
    connectedCallback() {
        if (this.authToken && this.checkoutId) {
            this.updateStore();
        }
    }
    componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    checkoutComplete(_event) {
        checkoutStore.isSubmitting = false;
    }
    checkoutError(_event) {
        checkoutStore.isSubmitting = false;
    }
    checkoutChanged(event) {
        // if disabled bnpl, remove sezzle from available payment methods
        if (this.disableBnpl) {
            this.availablePaymentMethods = event.detail.availablePaymentMethodTypes.filter((method) => method !== PAYMENT_METHODS.SEZZLE);
            return;
        }
        this.availablePaymentMethods = event.detail.availablePaymentMethodTypes;
    }
    async fillBillingForm(fields) {
        checkoutStore.billingFormFields = fields;
    }
    async validate() {
        var _a;
        const modularValidation = await ((_a = this.modularCheckoutRef) === null || _a === void 0 ? void 0 : _a.validate());
        return { isValid: modularValidation };
    }
    updateStore() {
        checkoutStore.checkoutId = this.checkoutId;
        checkoutStore.authToken = this.authToken;
        checkoutStore.disableCreditCard = this.disableCreditCard;
        checkoutStore.disableBankAccount = this.disableBankAccount;
        checkoutStore.disablePaymentMethodGroup = this.disablePaymentMethodGroup;
    }
    async submit(_event) {
        var _a;
        (_a = this.modularCheckoutRef) === null || _a === void 0 ? void 0 : _a.submitCheckout(checkoutStore.billingFormFields);
    }
    get showPaymentTypeHeader() {
        return !this.disableCreditCard && !this.disableBankAccount;
    }
    render() {
        var _a, _b;
        return (h(StyledHost, { key: 'bc8428227b2fc0e0753c4ef4e2370cd34bd34e82' }, h("justifi-modular-checkout", { key: '9fda18728d6fb5b20716070cefc2fcf8ef097459', ref: (el) => {
                this.modularCheckoutRef = el;
            }, authToken: this.authToken, checkoutId: this.checkoutId, preCompleteHook: this.preCompleteHook }, h("div", { key: 'e39f9871696d44cea2080198c7b91d03b706f2fd', class: "row gy-3 jfi-checkout-core" }, h("div", { key: 'df6045e9394b10f9b3e4b86e1a2242db30cc9b7b', class: "col-12", part: checkoutSummary }, h("justifi-header", { key: '8f4332bd43371106faf4e264fd5b27c97cb589aa', text: "Summary", level: "h2", class: "fs-5 fw-bold pb-3" }), h("section", { key: 'a7170276f18b9fa4b085c4d5de35979d55935929' }, h("justifi-checkout-summary", { key: '109aceb53ad2500e927230b3f82bbb07ab3f604c' }))), h("div", { key: '902e040bbd591408c9d292a592ca4d31fc838d85', class: "col-12 mt-4" }, h("slot", { key: '2677cb4952d88a8ce8f0198b96ed2638d4f4e6eb', name: "insurance" })), h("div", { key: '32c033bba12a7b7bf26159bd1878d6fa4ed913d9', class: "col-12 mt-4" }, h("justifi-header", { key: '74543b63c1b94a5200ad36236818fce7345d5d76', text: "Payment", level: "h2", class: "fs-5 fw-bold pb-3" }), this.showPaymentTypeHeader && (h("justifi-header", { key: '8e7c56c75bb779211b72c0b8561df8283b731ca1', text: "Select payment type", level: "h3", class: "fs-6 fw-bold lh-lg" })), h("div", { key: 'c2b0a875169b46dcd7d73428e3f1a4825b380ce5', class: "d-flex flex-column" }, h("section", { key: 'c5f007674c058e88d271c17f0b6b9c959bd0e044' }, h("div", { key: '2c4ecdd0f8708f9f80bae50362ba7fab295f788f' }, this.availablePaymentMethods.includes(PAYMENT_METHODS.APPLE_PAY) && (h("div", { key: '5ac363a3b7c38cb3f10e9c2c7916825f0f5dc2d8', class: "mb-3" }, h("justifi-apple-pay", { key: '498c03630e4057b54ebd12a3cd87b6b6bdb00c4b' }))), this.availablePaymentMethods.includes(PAYMENT_METHODS.GOOGLE_PAY) && (h("div", { key: '8c8114d142927739a3217d3a0a44c34c89e58f83', class: "mb-3" }, h("justifi-google-pay", Object.assign({ key: 'a2fbc5b99a83fbc52698422d2f57a3380f97733f', ref: (el) => (this.googlePayRef = el) }, (this.googlePayEnv != null ? { environment: this.googlePayEnv } : {}))))), h("justifi-saved-payment-methods", { key: '2e5b27eea7ef433e950feadae0b0a07f8b613533' }), this.availablePaymentMethods.includes(PAYMENT_METHODS.SEZZLE) && (h("justifi-radio-list-item", { key: 'fb1501c51eaf836579547bc50c295ebb67cb09c1', name: "paymentMethodType", value: PAYMENT_METHODS.SEZZLE, checked: ((_a = checkoutStore === null || checkoutStore === void 0 ? void 0 : checkoutStore.selectedPaymentMethod) === null || _a === void 0 ? void 0 : _a.type) === PAYMENT_METHODS.SEZZLE, label: h("justifi-sezzle-payment-method", { ref: (el) => (this.sezzlePaymentMethodRef = el) }), "onRadio-click": () => { var _a; (_a = this.sezzlePaymentMethodRef) === null || _a === void 0 ? void 0 : _a.handleSelectionClick(); } })), this.availablePaymentMethods.includes(PAYMENT_METHODS.PLAID) && !this.disableBnpl && (h("justifi-radio-list-item", { key: '49e0687b4955fb92935783250540fe38350bbdcc', name: "paymentMethodType", value: PAYMENT_METHODS.PLAID, checked: ((_b = checkoutStore === null || checkoutStore === void 0 ? void 0 : checkoutStore.selectedPaymentMethod) === null || _b === void 0 ? void 0 : _b.type) === PAYMENT_METHODS.PLAID, label: h("justifi-plaid-payment-method", { ref: (el) => (this.plaidPaymentMethodRef = el) }), "onRadio-click": () => { var _a; (_a = this.plaidPaymentMethodRef) === null || _a === void 0 ? void 0 : _a.handleSelectionClick(); } })), h("justifi-tokenize-payment-method", { key: 'ba793274de073c8a60c063e9caedcf32780b4bbf', ref: (el) => (this.tokenizePaymentMethodRef = el), authToken: this.authToken, accountId: checkoutStore.accountId, disableCreditCard: this.disableCreditCard, disableBankAccount: this.disableBankAccount, hideCardBillingForm: this.hideCardBillingForm, hideBankAccountBillingForm: this.hideBankAccountBillingForm, hideSubmitButton: true, paymentMethodGroupId: checkoutStore.paymentMethodGroupId }))))), h("div", { key: 'ed7be422c4e369382fd42e0b0162357df7de2031', class: "mt-4" }, h("justifi-button", { key: '849d648feec5ce805d6eb87acacf94d62e7a8f1b', text: "Pay", type: "submit", variant: "primary", clickHandler: (e) => this.submit(e), disabled: checkoutStore.isSubmitting || checkoutStore.isWalletProcessing, isLoading: checkoutStore.isSubmitting || checkoutStore.isWalletProcessing, customStyle: { width: '100%', textAlign: "center" } }))))));
    }
    static get is() { return "justifi-checkout"; }
    static get properties() {
        return {
            "authToken": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "auth-token"
            },
            "checkoutId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "checkout-id"
            },
            "disableBankAccount": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disable-bank-account",
                "defaultValue": "false"
            },
            "disableBnpl": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disable-bnpl",
                "defaultValue": "false"
            },
            "disableCreditCard": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disable-credit-card",
                "defaultValue": "false"
            },
            "disablePaymentMethodGroup": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disable-payment-method-group",
                "defaultValue": "false"
            },
            "hideBankAccountBillingForm": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "hide-bank-account-billing-form",
                "defaultValue": "false"
            },
            "hideCardBillingForm": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "hide-card-billing-form",
                "defaultValue": "false"
            },
            "googlePayEnv": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'TEST' | 'PRODUCTION'",
                    "resolved": "\"PRODUCTION\" | \"TEST\"",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Passed to `justifi-google-pay` as `environment`. Omit to let the child use its default."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "google-pay-env"
            },
            "preCompleteHook": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Hook<CheckoutState>",
                    "resolved": "(data: CheckoutState, resolve: (data: CheckoutState) => void, reject: () => void) => void",
                    "references": {
                        "Hook": {
                            "location": "import",
                            "path": "../modular-checkout/ModularCheckout",
                            "id": "src/components/modular-checkout/ModularCheckout.ts::Hook"
                        },
                        "CheckoutState": {
                            "location": "import",
                            "path": "../../store/checkout.store",
                            "id": "src/store/checkout.store.ts::CheckoutState"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "availablePaymentMethods": {},
            "checkout": {},
            "complete": {},
            "errorMessage": {},
            "insuranceToggled": {},
            "serverError": {}
        };
    }
    static get events() {
        return [{
                "method": "errorEvent",
                "name": "error-event",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "ComponentErrorEvent",
                    "resolved": "ComponentErrorEvent",
                    "references": {
                        "ComponentErrorEvent": {
                            "location": "import",
                            "path": "../../api/ComponentEvents",
                            "id": "src/api/ComponentEvents.ts::ComponentErrorEvent"
                        }
                    }
                }
            }, {
                "method": "loadedEvent",
                "name": "loaded",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "ILoadedEventResponse",
                    "resolved": "{ checkout_status: ICheckoutStatus; }",
                    "references": {
                        "ILoadedEventResponse": {
                            "location": "import",
                            "path": "../../api",
                            "id": "src/api/index.ts::ILoadedEventResponse"
                        }
                    }
                }
            }, {
                "method": "submitEvent",
                "name": "submit-event",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "ComponentSubmitEvent",
                    "resolved": "ComponentSubmitEvent",
                    "references": {
                        "ComponentSubmitEvent": {
                            "location": "import",
                            "path": "../../api/ComponentEvents",
                            "id": "src/api/ComponentEvents.ts::ComponentSubmitEvent"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "fillBillingForm": {
                "complexType": {
                    "signature": "(fields: BillingFormFields) => Promise<void>",
                    "parameters": [{
                            "name": "fields",
                            "type": "BillingFormFields",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "BillingFormFields": {
                            "location": "import",
                            "path": "./billing-form/billing-form-schema",
                            "id": "src/components/checkout/billing-form/billing-form-schema.ts::BillingFormFields"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "validate": {
                "complexType": {
                    "signature": "() => Promise<{ isValid: boolean; }>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<{ isValid: boolean; }>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get watchers() {
        return [{
                "propName": "authToken",
                "methodName": "propChanged"
            }, {
                "propName": "checkoutId",
                "methodName": "propChanged"
            }, {
                "propName": "disableCreditCard",
                "methodName": "propChanged"
            }, {
                "propName": "disableBankAccount",
                "methodName": "propChanged"
            }, {
                "propName": "disablePaymentMethodGroup",
                "methodName": "propChanged"
            }];
    }
    static get listeners() {
        return [{
                "name": "submit-event",
                "method": "checkoutComplete",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "error-event",
                "method": "checkoutError",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "checkout-changed",
                "method": "checkoutChanged",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
