import { h, r as registerInstance, c as createEvent } from './index-CV0Pyo1O.js';
import { c as create$3, F as FormController } from './index.esm-KCSohgHx.js';
import { b as businessCoreInfoSchemaUSA, i as identitySchemaUSA, a as additionalQuestionsSchema, c as addressSchemaUSA, d as businessCoreInfoSchemaCAN, e as identitySchemaCAN, f as addressSchemaCAN } from './business-identity-schema-C1ot9Zas.js';
import './config-state-kKn89dp1.js';
import './dinero-HXpYMWUU.js';
import { C as ComponentErrorSeverity, b as ComponentErrorCodes } from './Plaid-DxSkT8pY.js';
import './Pagination-CLHwhyvu.js';
import { C as CountryCode, B as Business, b as BusinessFormServerErrors } from './Business-BcNb9bVO.js';
import { J as JustifiAnalytics } from './Analytics-C76Y5Pqf.js';
import { B as Button } from './button-BvzOzV4l.js';
import { S as StyledHost } from './styled-host-D84RBv1W.js';
import { H as Header1 } from './header-1-evYxFIGN.js';
import './event-types-CZfYo5-1.js';
import { c as checkPkgVersion } from './check-pkg-version-D5BxTNyL.js';
import { S as Skeleton } from './skeleton-BEHo2ypu.js';
import { B as BusinessFormClickActions } from './event-types-XYgDHrVL.js';
import { m as makeGetBusiness, a as makePatchBusiness } from './payment-provisioning-actions-ByGe6983.js';
import { B as BusinessService } from './business.service-BpvtCKI5.js';
import './_commonjsHelpers-BFTU3MAI.js';
import './schema-validations-CHxw3FkH.js';
import './country-config-CEJVOOGe.js';
import './business-form-options-DXjZp4Rl.js';
import './index-cDVGjRkU.js';
import './utils-C25uIOK0.js';
import './package-CqcLgdsl.js';
import './Api-BulOmiMS.js';
import './parts-CPKMvyV_.js';
import './utils-CVYvs2hZ.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 = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        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"))))));
    }
};

export { JustifiBusinessForm as justifi_business_form };
