import { h, proxyCustomElement, HTMLElement, createEvent } from '@stencil/core/internal/client';
import { c as create$3, F as FormController } from './form.js';
import { a as businessCoreInfoSchemaUSA, b as businessCoreInfoSchemaCAN } from './business-core-info-schema.js';
import { a as additionalQuestionsSchema } from './business-additional-questions-schema.js';
import { a as identitySchemaUSA, b as identitySchemaCAN } from './business-identity-schema.js';
import { b as addressSchemaUSA, a as addressSchemaCAN } from './business-address-schema.js';
import './config-state.js';
import './dinero.js';
import { C as ComponentErrorSeverity, a as ComponentErrorCodes } from './Plaid.js';
import './Pagination.js';
import { a as CountryCode, b as Business, e as BusinessFormServerErrors } from './Business.js';
import './ApplePay.js';
import { J as JustifiAnalytics } from './Analytics.js';
import { B as Button } from './button.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 { c as checkPkgVersion } from './check-pkg-version.js';
import { S as Skeleton } from './skeleton.js';
import { a as BusinessFormClickActions } from './event-types2.js';
import { m as makeGetBusiness, a as makePatchBusiness } from './payment-provisioning-actions.js';
import { B as BusinessService } from './business.service.js';
import { d as defineCustomElement$f } from './additional-questions2.js';
import { d as defineCustomElement$e } from './business-core-info2.js';
import { d as defineCustomElement$d } from './business-representative2.js';
import { d as defineCustomElement$c } from './custom-popper2.js';
import { d as defineCustomElement$b } from './form-address-fields2.js';
import { d as defineCustomElement$a } from './form-alert2.js';
import { d as defineCustomElement$9 } from './form-control-date2.js';
import { d as defineCustomElement$8 } from './form-control-monetary-provisioning2.js';
import { d as defineCustomElement$7 } from './form-control-number-masked2.js';
import { d as defineCustomElement$6 } from './form-control-select2.js';
import { d as defineCustomElement$5 } from './form-control-text2.js';
import { d as defineCustomElement$4 } from './form-control-tooltip2.js';
import { d as defineCustomElement$3 } from './identity-address-form2.js';
import { d as defineCustomElement$2 } from './legal-address-form2.js';

const businessFormSchemaUSA = create$3(Object.assign(Object.assign({}, businessCoreInfoSchemaUSA().fields), { legal_address: addressSchemaUSA(), additional_questions: additionalQuestionsSchema(), representative: identitySchemaUSA('representative') }));
create$3(Object.assign(Object.assign({}, businessCoreInfoSchemaCAN().fields), { legal_address: addressSchemaCAN(), additional_questions: additionalQuestionsSchema(), representative: identitySchemaCAN('representative') }));
// For backward compatibility, default to USA
const businessFormSchema = businessFormSchemaUSA;

const FormBlock = () => (h("div", { class: "row gap-3" }, h("div", { class: "col-12" }, h(Skeleton, { width: "25%" })), h("div", { class: "col-12" }, h(Skeleton, { width: "100%" })), h("div", { class: "col-12 d-flex gap-3" }, h(Skeleton, { width: "50%" }), h(Skeleton, { width: "50%" })), h("div", { class: "col-12 d-flex gap-3" }, h(Skeleton, { width: "65%" }), h(Skeleton, { width: "35%" }))));
const BusinessFormLoading = () => {
    return (h("div", { class: "row gap-5" }, h("div", { class: "col-12" }, h(Skeleton, { width: "40%", height: "40px" })), h(FormBlock, null), h(FormBlock, null), h(FormBlock, null)));
};

const JustifiBusinessForm$1 = /*@__PURE__*/ proxyCustomElement(class JustifiBusinessForm extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        this.submitEvent = createEvent(this, "submit-event", 7);
        this.clickEvent = createEvent(this, "click-event", 7);
        this.errorEvent = createEvent(this, "error-event", 7);
        this.isLoading = false;
        this.isSaving = false;
        this.country = CountryCode.USA;
        this.hideErrors = false;
        this.formTitle = 'Business Information';
        this.removeTitle = false;
        this.instantiateBusiness = (data) => {
            const business = new Business(data);
            this.country = business.country_of_establishment;
            this.formController.setInitialValues(Object.assign({}, business));
        };
        this.sendData = async () => {
            this.isSaving = true;
            const values = this.formController.values.getValue();
            const initialValues = this.formController.getInitialValues();
            const payload = new Business(Object.assign(Object.assign({}, initialValues), values)).payload;
            this.patchBusiness({
                payload,
                onSuccess: (response) => {
                    this.handleReponse(response);
                },
                onError: ({ error, code, severity }) => {
                    this.errorMessage = error.message;
                    this.errorEvent.emit({ errorCode: code, message: error.message, severity });
                },
                final: () => {
                    this.isSaving = false;
                }
            });
        };
        this.validateAndSubmit = (event) => {
            event.preventDefault();
            this.formController.validateAndSubmit(this.sendData);
        };
    }
    componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
        this.formController = new FormController(businessFormSchema);
        this.initializeApi();
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    ;
    get title() {
        return this.removeTitle ? '' : this.formTitle;
    }
    get disabledState() {
        return this.isSaving;
    }
    get businessEndpoint() {
        return `entities/business/${this.businessId}`;
    }
    initializeApi() {
        if (this.authToken && this.businessId) {
            this.getBusiness = makeGetBusiness({
                authToken: this.authToken,
                businessId: this.businessId,
                service: new BusinessService()
            });
            this.patchBusiness = makePatchBusiness({
                authToken: this.authToken,
                businessId: this.businessId,
                service: new BusinessService()
            });
            this.getBusiness({
                onSuccess: (response) => {
                    this.instantiateBusiness(response.data);
                },
                onError: ({ error, code, severity }) => {
                    this.errorMessage = error.message;
                    this.errorEvent.emit({ errorCode: code, message: error.message, severity });
                },
                final: () => {
                    this.isLoading = false;
                }
            });
        }
        else {
            this.errorEvent.emit({
                message: 'auth-token and business-id are required',
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    handleReponse(response) {
        if (response.error) {
            this.errorMessage = BusinessFormServerErrors.patchData;
        }
        this.submitEvent.emit({ response });
        this.instantiateBusiness(response.data);
    }
    render() {
        if (this.isLoading) {
            return h(StyledHost, null, h(BusinessFormLoading, null));
        }
        return (h(StyledHost, null, h("form", { onSubmit: this.validateAndSubmit }, h("div", { class: "row gap-3" }, h(Header1, { text: this.title }), h("form-alert", { text: this.errorMessage, hideAlert: this.hideErrors }), h("div", { class: "col-12 mb-4" }, h("business-core-info", { formController: this.formController, country: this.country })), h("div", { class: "col-12 mb-4" }, h("legal-address-form", { formController: this.formController, country: this.country })), h("div", { class: "col-12 mb-4" }, h("additional-questions", { formController: this.formController })), h("div", { class: "col-12 mb-4" }, h("business-representative", { formController: this.formController, country: this.country })), h("div", { class: "col-12 d-flex flex-row-reverse" }, h(Button, { type: "submit", disabled: this.disabledState, variant: 'primary', onClick: () => this.clickEvent.emit({ name: BusinessFormClickActions.submit }), isLoading: this.isSaving }, "Submit"))))));
    }
}, [257, "justifi-business-form", {
        "authToken": [1, "auth-token"],
        "businessId": [1, "business-id"],
        "hideErrors": [4, "hide-errors"],
        "formTitle": [1, "form-title"],
        "removeTitle": [4, "remove-title"],
        "isLoading": [32],
        "isSaving": [32],
        "errorMessage": [32],
        "country": [32]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["justifi-business-form", "additional-questions", "business-core-info", "business-representative", "custom-popper", "form-address-fields", "form-alert", "form-control-date", "form-control-monetary-provisioning", "form-control-number-masked", "form-control-select", "form-control-text", "form-control-tooltip", "identity-address-form", "legal-address-form"];
    components.forEach(tagName => { switch (tagName) {
        case "justifi-business-form":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, JustifiBusinessForm$1);
            }
            break;
        case "additional-questions":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "business-core-info":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "business-representative":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "form-address-fields":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "form-alert":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "form-control-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "form-control-monetary-provisioning":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "form-control-number-masked":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "form-control-select":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "identity-address-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "legal-address-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const JustifiBusinessForm = JustifiBusinessForm$1;
const defineCustomElement = defineCustomElement$1;

export { JustifiBusinessForm, defineCustomElement };
