import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { C as ComponentErrorSeverity, a as ComponentErrorCodes } from './Plaid.js';
import { m as makeGetBusiness, a as makePatchBusiness } from './payment-provisioning-actions.js';
import { B as BusinessService } from './business.service.js';
import { R as Representative } from './Business.js';
import { F as FormController } from './form.js';
import { i as identitySchemaByCountry } from './business-identity-schema.js';
import { P as PaymentProvisioningLoading } from './payment-provisioning-loading.js';
import { B as BusinessFormStep } from './event-types2.js';
import { d as defineCustomElement$9 } from './business-representative-form-inputs2.js';
import { d as defineCustomElement$8 } from './custom-popper2.js';
import { d as defineCustomElement$7 } from './form-address-fields2.js';
import { d as defineCustomElement$6 } from './form-control-date2.js';
import { d as defineCustomElement$5 } from './form-control-number-masked2.js';
import { d as defineCustomElement$4 } from './form-control-select2.js';
import { d as defineCustomElement$3 } from './form-control-text2.js';
import { d as defineCustomElement$2 } from './form-control-tooltip2.js';
import { d as defineCustomElement$1 } from './identity-address-form2.js';

const BusinessRepresentativeFormStep = /*@__PURE__*/ proxyCustomElement(class BusinessRepresentativeFormStep extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.errorEvent = createEvent(this, "error-event", 7);
        this.stepCompleteEvent = createEvent(this, "complete-form-step-event", 7);
        this.formLoading = createEvent(this, "formLoading", 7);
        this.errors = {};
        this.representative = {};
        this.isLoading = false;
        this.getData = () => {
            if (!this.getBusiness || !this.formController)
                return;
            this.formLoading.emit(true);
            this.isLoading = true;
            this.getBusiness({
                onSuccess: (response) => {
                    this.representative = new Representative(response.data.representative || {});
                    this.representative.address.country = this.country;
                    this.formController.setInitialValues(Object.assign({}, this.representative));
                },
                onError: ({ error, code, severity }) => {
                    this.errorEvent.emit({
                        message: error,
                        errorCode: code,
                        severity: severity
                    });
                },
                final: () => {
                    this.formLoading.emit(false);
                    this.isLoading = false;
                }
            });
        };
        this.sendData = (onSuccess) => {
            let submittedData;
            this.formLoading.emit(true);
            this.patchBusiness({
                payload: this.patchPayload,
                onSuccess: (response) => {
                    submittedData = response;
                    onSuccess();
                },
                onError: ({ error, code, severity }) => {
                    submittedData = error;
                    this.errorEvent.emit({
                        message: error,
                        errorCode: code,
                        severity: severity
                    });
                },
                final: () => {
                    this.stepCompleteEvent.emit({ response: submittedData, formStep: BusinessFormStep.representative });
                    this.formLoading.emit(false);
                }
            });
        };
    }
    propChanged() {
        this.initializeApi();
        this.getData();
    }
    async validateAndSubmit({ onSuccess }) {
        this.formController.validateAndSubmit(() => this.sendData(onSuccess));
    }
    componentWillLoad() {
        this.initializeApi();
        this.initializeForm();
    }
    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()
            });
        }
        else {
            this.errorEvent.emit({
                message: 'Missing required props',
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    initializeForm() {
        const schemaFactory = identitySchemaByCountry[this.country];
        if (!schemaFactory) {
            this.errorEvent.emit({
                message: 'Missing schema for selected country',
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR,
            });
            return;
        }
        this.formController = new FormController(schemaFactory('representative', this.allowOptionalFields));
        this.getData();
    }
    componentDidLoad() {
        if (!this.formController)
            return;
        this.formController.errors.subscribe(errors => {
            this.errors = Object.assign({}, errors);
        });
    }
    get patchPayload() {
        const formValues = new Representative(this.formController.values.getValue()).payload;
        return { representative: formValues };
    }
    render() {
        if (!this.formController || this.isLoading) {
            return (h(PaymentProvisioningLoading, null));
        }
        return (h("business-representative-form-inputs", { representativeDefaultValue: this.formController.getInitialValues(), errors: this.errors, formController: this.formController, country: this.country }));
    }
    static get watchers() { return {
        "authToken": ["propChanged"],
        "businessId": ["propChanged"]
    }; }
}, [256, "business-representative-form-step", {
        "authToken": [1, "auth-token"],
        "businessId": [1, "business-id"],
        "allowOptionalFields": [4, "allow-optional-fields"],
        "country": [1],
        "getBusiness": [32],
        "patchBusiness": [32],
        "formController": [32],
        "errors": [32],
        "representative": [32],
        "isLoading": [32],
        "validateAndSubmit": [64]
    }, undefined, {
        "authToken": ["propChanged"],
        "businessId": ["propChanged"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["business-representative-form-step", "business-representative-form-inputs", "custom-popper", "form-address-fields", "form-control-date", "form-control-number-masked", "form-control-select", "form-control-text", "form-control-tooltip", "identity-address-form"];
    components.forEach(tagName => { switch (tagName) {
        case "business-representative-form-step":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, BusinessRepresentativeFormStep);
            }
            break;
        case "business-representative-form-inputs":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "form-address-fields":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "form-control-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "form-control-number-masked":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "form-control-select":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "identity-address-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { BusinessRepresentativeFormStep as B, defineCustomElement as d };
