import { h, proxyCustomElement, HTMLElement, createEvent } from '@stencil/core/internal/client';
import { J as JustifiAnalytics } from './Analytics.js';
import { C as ComponentErrorSeverity, a as ComponentErrorCodes } from './Plaid.js';
import { m as makeGetBusiness, g as makePostProvisioning } from './payment-provisioning-actions.js';
import { B as BusinessService } from './business.service.js';
import { A as Api } from './Api.js';
import './Pagination.js';
import { a as CountryCode, b as Business, f as checkProvisioningStatus } from './Business.js';
import './ApplePay.js';
import { c as checkPkgVersion } from './check-pkg-version.js';
import { n as isTokenExpired } from './utils2.js';
import { j as text } from './parts.js';
import { S as StyledHost } from './styled-host.js';
import { H as Header1 } from './header-1.js';
import './justifi-details2.js';
import './event-types.js';
import './justifi-radio-list-item2.js';
import { a as BusinessFormClickActions } from './event-types2.js';
import { P as PaymentProvisioningLoading } from './payment-provisioning-loading.js';
import { d as defineCustomElement$w } from './additional-questions-form-step2.js';
import { d as defineCustomElement$v } from './bank-account-form-inputs2.js';
import { d as defineCustomElement$u } from './bank-account-form-inputs-canada2.js';
import { d as defineCustomElement$t } from './bank-account-read-only-value2.js';
import { d as defineCustomElement$s } from './business-bank-account-form-step2.js';
import { d as defineCustomElement$r } from './business-core-info-form-step2.js';
import { d as defineCustomElement$q } from './business-documents-on-file2.js';
import { d as defineCustomElement$p } from './business-owners-form-step2.js';
import { d as defineCustomElement$o } from './business-representative-form-inputs2.js';
import { d as defineCustomElement$n } from './business-representative-form-step2.js';
import { d as defineCustomElement$m } from './business-terms-conditions-form-step2.js';
import { d as defineCustomElement$l } from './custom-popper2.js';
import { d as defineCustomElement$k } from './document-upload-form-step2.js';
import { d as defineCustomElement$j } from './document-upload-list2.js';
import { d as defineCustomElement$i } from './form-address-fields2.js';
import { d as defineCustomElement$h } from './form-control-checkbox2.js';
import { d as defineCustomElement$g } from './form-control-date2.js';
import { d as defineCustomElement$f } from './form-control-file2.js';
import { d as defineCustomElement$e } from './form-control-monetary-provisioning2.js';
import { d as defineCustomElement$d } from './form-control-number2.js';
import { d as defineCustomElement$c } from './form-control-number-masked2.js';
import { d as defineCustomElement$b } from './form-control-select2.js';
import { d as defineCustomElement$a } from './form-control-text2.js';
import { d as defineCustomElement$9 } from './form-control-tooltip2.js';
import { d as defineCustomElement$8 } from './identity-address-form2.js';
import { d as defineCustomElement$7 } from './legal-address-form-step2.js';
import { d as defineCustomElement$6 } from './owner-form2.js';
import { d as defineCustomElement$5 } from './owner-form-inputs2.js';
import { d as defineCustomElement$4 } from './payment-provisioning-form-buttons2.js';
import { d as defineCustomElement$3 } from './payment-provisioning-form-steps2.js';
import { d as defineCustomElement$2 } from './plaid-verification2.js';

const api = Api();
class ProvisionService {
    async postProvisioning(authToken, businessId, product) {
        const endpoint = `entities/provisioning`;
        const body = {
            business_id: businessId,
            product_category: product,
        };
        return api.post({ endpoint, body, authToken });
    }
}

const PaymentProvisioningAlreadyProvisioned = () => {
    return (h("div", { class: "row gap-3 p-5" }, h("div", { class: "col-12" }, h(Header1, { text: "Business Already Provisioned" })), h("div", { class: "col-12", part: text }, h("p", null, "We have already received the onboarding information for this business."))));
};

const PaymentProvisioningSubmissionComplete = () => {
    return (h("div", { class: "row gap-3 p5" }, h("div", { class: "col-12" }, h(Header1, { text: "You're all set for now" })), h("div", { class: "col-12", part: text }, h("p", null, "We're reviewing your information and working on your approval. We'll be in touch to let you know once you've been approved (or if we find anything that needs fixing). Have a good one!"))));
};

const PaymentProvisioningSubmissionFailed = () => {
    return (h("div", { class: "row gap-3 p-5" }, h("div", { class: "col-12" }, h(Header1, { text: "Something went wrong" })), h("div", { class: "col-12", part: text }, h("p", null, "There was an error submitting the provisioning. Your progress has been saved \u2014 please try again later or contact the support team."))));
};

const JustifiPaymentProvisioning$1 = /*@__PURE__*/ proxyCustomElement(class JustifiPaymentProvisioning extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        this.clickEvent = createEvent(this, "click-event", 7);
        this.errorEvent = createEvent(this, "error-event", 7);
        this.submitEvent = createEvent(this, "submit-event", 7);
        this.loading = false;
        this.businessLoading = true;
        this.businessProvisioned = false;
        this.formSubmitted = false;
        this.submissionFailed = false;
        this.currentStep = 0;
        this.country = CountryCode.USA;
        this.allowOptionalFields = false;
        this.formTitle = 'Business Information';
        this.refs = [];
        this.setBusinessProvisioned = () => {
            return new Promise((resolve) => {
                this.getBusiness({
                    onSuccess: (response) => {
                        const business = new Business(response.data);
                        this.businessProvisioned = checkProvisioningStatus(business);
                        this.country = business.country_of_establishment;
                        if (this.businessProvisioned) {
                            this.errorEvent.emit({
                                message: 'A request to provision payments for this business has already been submitted.',
                                errorCode: ComponentErrorCodes.PROVISIONING_REQUESTED,
                                severity: ComponentErrorSeverity.INFO,
                            });
                        }
                        this.businessLoading = false;
                        resolve();
                    },
                    onError: ({ error, code, severity }) => {
                        this.errorEvent.emit({
                            message: error,
                            errorCode: code,
                            severity: severity,
                        });
                        this.businessLoading = false;
                        resolve();
                    },
                });
            });
        };
        this.postProvisioningData = () => {
            this.postProvisioning({
                onSuccess: (response) => {
                    this.formSubmitted = true;
                    this.submitEvent.emit({ response: response });
                },
                onError: ({ error, code, severity }) => {
                    this.submissionFailed = true;
                    this.submitEvent.emit({ response: { error } });
                    this.errorEvent.emit({
                        message: error,
                        errorCode: code,
                        severity: severity,
                    });
                },
            });
        };
        this.incrementSteps = () => {
            if (this.currentStep < this.totalSteps) {
                return this.currentStep++;
            }
        };
        this.decrementSteps = () => {
            if (this.currentStep > 0) {
                this.currentStep -= 1;
            }
        };
        this.previousStepButtonOnClick = () => {
            this.clickEvent.emit({ name: BusinessFormClickActions.previousStep });
            this.decrementSteps();
        };
        this.nextStepButtonOnClick = (e, clickEventName) => {
            e.preventDefault();
            this.clickEvent.emit({ name: clickEventName });
            const currentStep = this.refs[this.currentStep];
            currentStep.validateAndSubmit({ onSuccess: this.incrementSteps });
        };
    }
    propChanged() {
        this.initializeApi();
    }
    handleFormCompleted() {
        this.postProvisioningData();
    }
    handleFormLoading(event) {
        this.loading = event.detail;
    }
    async componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
        this.initializeApi();
        if (this.getBusiness) {
            await this.setBusinessProvisioned();
        }
        else {
            this.businessLoading = false;
        }
        this.refs = new Array(8);
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    initializeApi() {
        if (!this.authToken || !this.businessId) {
            this.errorEvent.emit({
                message: 'auth-token and business-id are required',
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR,
            });
            return;
        }
        if (isTokenExpired(this.authToken)) {
            this.errorEvent.emit({
                message: 'auth-token is expired',
                errorCode: ComponentErrorCodes.NOT_AUTHENTICATED,
                severity: ComponentErrorSeverity.ERROR,
            });
            return;
        }
        this.getBusiness = makeGetBusiness({
            authToken: this.authToken,
            businessId: this.businessId,
            service: new BusinessService(),
        });
        this.postProvisioning = makePostProvisioning({
            authToken: this.authToken,
            businessId: this.businessId,
            product: 'payment',
            service: new ProvisionService(),
        });
    }
    get formDisabled() {
        return this.loading || this.businessProvisioned;
    }
    get totalSteps() {
        return this.refs.length - 1;
    }
    get stepCounter() {
        return `Step ${this.currentStep + 1} of ${this.totalSteps + 1}`;
    }
    render() {
        if (this.businessLoading) {
            return (h(StyledHost, null, h(PaymentProvisioningLoading, null)));
        }
        if (this.businessProvisioned) {
            return (h(StyledHost, null, h(PaymentProvisioningAlreadyProvisioned, null)));
        }
        if (this.formSubmitted) {
            return (h(StyledHost, null, h(PaymentProvisioningSubmissionComplete, null)));
        }
        if (this.submissionFailed) {
            return (h(StyledHost, null, h(PaymentProvisioningSubmissionFailed, null)));
        }
        return (h(StyledHost, null, h("div", { class: "row gap-3" }, h(Header1, { text: this.formTitle }), h("payment-provisioning-form-steps", { businessId: this.businessId, authToken: this.authToken, refs: this.refs, currentStep: this.currentStep, allowOptionalFields: this.allowOptionalFields, country: this.country, handleFormLoading: this.handleFormLoading }), h("div", { class: "d-flex justify-content-between align-items-center" }, h("div", { class: "d-flex align-items-center", part: text }, this.stepCounter), h("payment-provisioning-form-buttons", { currentStep: this.currentStep, totalSteps: this.totalSteps, formLoading: this.loading, formDisabled: this.formDisabled, previousStepButtonOnClick: this.previousStepButtonOnClick, nextStepButtonOnClick: this.nextStepButtonOnClick })))));
    }
    static get watchers() { return {
        "authToken": ["propChanged"],
        "businessId": ["propChanged"]
    }; }
}, [257, "justifi-payment-provisioning", {
        "businessId": [1, "business-id"],
        "authToken": [1, "auth-token"],
        "allowOptionalFields": [4, "allow-optional-fields"],
        "formTitle": [1, "form-title"],
        "loading": [32],
        "businessLoading": [32],
        "businessProvisioned": [32],
        "formSubmitted": [32],
        "submissionFailed": [32],
        "currentStep": [32],
        "errorMessage": [32],
        "country": [32]
    }, [[0, "formCompleted", "handleFormCompleted"], [0, "formLoading", "handleFormLoading"]], {
        "authToken": ["propChanged"],
        "businessId": ["propChanged"]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["justifi-payment-provisioning", "additional-questions-form-step", "bank-account-form-inputs", "bank-account-form-inputs-canada", "bank-account-read-only-value", "business-bank-account-form-step", "business-core-info-form-step", "business-documents-on-file", "business-owners-form-step", "business-representative-form-inputs", "business-representative-form-step", "business-terms-conditions-form-step", "custom-popper", "document-upload-form-step", "document-upload-list", "form-address-fields", "form-control-checkbox", "form-control-date", "form-control-file", "form-control-monetary-provisioning", "form-control-number", "form-control-number-masked", "form-control-select", "form-control-text", "form-control-tooltip", "identity-address-form", "legal-address-form-step", "owner-form", "owner-form-inputs", "payment-provisioning-form-buttons", "payment-provisioning-form-steps", "plaid-verification"];
    components.forEach(tagName => { switch (tagName) {
        case "justifi-payment-provisioning":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, JustifiPaymentProvisioning$1);
            }
            break;
        case "additional-questions-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$w();
            }
            break;
        case "bank-account-form-inputs":
            if (!customElements.get(tagName)) {
                defineCustomElement$v();
            }
            break;
        case "bank-account-form-inputs-canada":
            if (!customElements.get(tagName)) {
                defineCustomElement$u();
            }
            break;
        case "bank-account-read-only-value":
            if (!customElements.get(tagName)) {
                defineCustomElement$t();
            }
            break;
        case "business-bank-account-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$s();
            }
            break;
        case "business-core-info-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$r();
            }
            break;
        case "business-documents-on-file":
            if (!customElements.get(tagName)) {
                defineCustomElement$q();
            }
            break;
        case "business-owners-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$p();
            }
            break;
        case "business-representative-form-inputs":
            if (!customElements.get(tagName)) {
                defineCustomElement$o();
            }
            break;
        case "business-representative-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$n();
            }
            break;
        case "business-terms-conditions-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$m();
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$l();
            }
            break;
        case "document-upload-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$k();
            }
            break;
        case "document-upload-list":
            if (!customElements.get(tagName)) {
                defineCustomElement$j();
            }
            break;
        case "form-address-fields":
            if (!customElements.get(tagName)) {
                defineCustomElement$i();
            }
            break;
        case "form-control-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$h();
            }
            break;
        case "form-control-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$g();
            }
            break;
        case "form-control-file":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "form-control-monetary-provisioning":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "form-control-number":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "form-control-number-masked":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "form-control-select":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "identity-address-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "legal-address-form-step":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "owner-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "owner-form-inputs":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "payment-provisioning-form-buttons":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "payment-provisioning-form-steps":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "plaid-verification":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const JustifiPaymentProvisioning = JustifiPaymentProvisioning$1;
const defineCustomElement = defineCustomElement$1;

export { JustifiPaymentProvisioning, defineCustomElement };
