'use strict';

var index = require('./index-p7cvUYAE.js');
var index_esm = require('./index.esm-BNETBIV_.js');
var businessIdentitySchema = require('./business-identity-schema-Da3PJrGG.js');
require('./config-state-BIgGhhe9.js');
require('./dinero-BwNN_k7x.js');
var Plaid = require('./Plaid-CPESyiiV.js');
require('./Pagination-Dediz133.js');
var Business = require('./Business-oBifyXkW.js');
var Analytics = require('./Analytics-C6KBHEIP.js');
var button = require('./button-CR9Rl1-o.js');
var styledHost = require('./styled-host-Ch0-dxF-.js');
var header1 = require('./header-1-CsBQoo1f.js');
require('./event-types-YOY5TKUN.js');
var checkPkgVersion = require('./check-pkg-version-B9EoYyZR.js');
var skeleton = require('./skeleton-DvRCiQlA.js');
var eventTypes = require('./event-types-DNOUef41.js');
var paymentProvisioningActions = require('./payment-provisioning-actions-P8cbgnKy.js');
var business_service = require('./business.service-BIHpEqmf.js');
require('./_commonjsHelpers-BJu3ubxk.js');
require('./schema-validations-Cs0mSKR7.js');
require('./country-config-C1bWmF8P.js');
require('./business-form-options-CnlrfbQ6.js');
require('./index-W-zoNiyM.js');
require('./utils-CYONRFR6.js');
require('./package-YyJzVqrh.js');
require('./Api-D9vOcUw4.js');
require('./parts-BXshb_S-.js');
require('./utils-BsnepLy6.js');

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

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

const JustifiBusinessForm = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.submitEvent = index.createEvent(this, "submit-event", 7);
        this.clickEvent = index.createEvent(this, "click-event", 7);
        this.errorEvent = index.createEvent(this, "error-event", 7);
        this.isLoading = false;
        this.isSaving = false;
        this.country = Business.CountryCode.USA;
        this.hideErrors = false;
        this.formTitle = 'Business Information';
        this.removeTitle = false;
        this.instantiateBusiness = (data) => {
            const business = new Business.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.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.checkPkgVersion();
        this.analytics = new Analytics.JustifiAnalytics(this);
        this.formController = new index_esm.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 = paymentProvisioningActions.makeGetBusiness({
                authToken: this.authToken,
                businessId: this.businessId,
                service: new business_service.BusinessService()
            });
            this.patchBusiness = paymentProvisioningActions.makePatchBusiness({
                authToken: this.authToken,
                businessId: this.businessId,
                service: new business_service.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: Plaid.ComponentErrorCodes.MISSING_PROPS,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    handleReponse(response) {
        if (response.error) {
            this.errorMessage = Business.BusinessFormServerErrors.patchData;
        }
        this.submitEvent.emit({ response });
        this.instantiateBusiness(response.data);
    }
    render() {
        if (this.isLoading) {
            return index.h(styledHost.StyledHost, null, index.h(BusinessFormLoading, null));
        }
        return (index.h(styledHost.StyledHost, null, index.h("form", { onSubmit: this.validateAndSubmit }, index.h("div", { class: "row gap-3" }, index.h(header1.Header1, { text: this.title }), index.h("form-alert", { text: this.errorMessage, hideAlert: this.hideErrors }), index.h("div", { class: "col-12 mb-4" }, index.h("business-core-info", { formController: this.formController, country: this.country })), index.h("div", { class: "col-12 mb-4" }, index.h("legal-address-form", { formController: this.formController, country: this.country })), index.h("div", { class: "col-12 mb-4" }, index.h("additional-questions", { formController: this.formController })), index.h("div", { class: "col-12 mb-4" }, index.h("business-representative", { formController: this.formController, country: this.country })), index.h("div", { class: "col-12 d-flex flex-row-reverse" }, index.h(button.Button, { type: "submit", disabled: this.disabledState, variant: 'primary', onClick: () => this.clickEvent.emit({ name: eventTypes.BusinessFormClickActions.submit }), isLoading: this.isSaving }, "Submit"))))));
    }
};

exports.justifi_business_form = JustifiBusinessForm;
