import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { F as FormController } from './form.js';
import { O as Owner } from './Business.js';
import { i as identitySchemaByCountry } from './business-identity-schema.js';
import { B as Button } from './button.js';
import { y as heading3 } from './parts.js';
import './justifi-details2.js';
import './event-types.js';
import './justifi-radio-list-item2.js';
import { a as BusinessFormClickActions } from './event-types2.js';
import { b as makeGetIdentity, c as makePatchIdentity, d as makePostIdentity } from './payment-provisioning-actions.js';
import { I as IdentityService } from './business.service.js';
import { d as defineCustomElement$a } from './custom-popper2.js';
import { d as defineCustomElement$9 } from './form-address-fields2.js';
import { d as defineCustomElement$8 } from './form-control-date2.js';
import { d as defineCustomElement$7 } from './form-control-number2.js';
import { d as defineCustomElement$6 } from './form-control-number-masked2.js';
import { d as defineCustomElement$5 } from './form-control-select2.js';
import { d as defineCustomElement$4 } from './form-control-text2.js';
import { d as defineCustomElement$3 } from './form-control-tooltip2.js';
import { d as defineCustomElement$2 } from './identity-address-form2.js';
import { d as defineCustomElement$1 } from './owner-form-inputs2.js';

const OwnerForm = /*@__PURE__*/ proxyCustomElement(class OwnerForm extends HTMLElement {
    loadingWatcher() {
        this.formLoading.emit(this.isLoading);
    }
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.clickEvent = createEvent(this, "click-event", 7);
        this.errorEvent = createEvent(this, "error-event", 7);
        this.formLoading = createEvent(this, "formLoading", 7);
        this.ownerSubmitted = createEvent(this, "ownerSubmitted", 7);
        this.isLoading = false;
        this.errors = {};
        this.owner = {};
        this.getData = () => {
            if (!this.getOwner) {
                return;
            }
            this.isLoading = true;
            this.getOwner({
                onSuccess: (response) => {
                    this.instantiateOwner(response.data);
                },
                onError: ({ error, code, severity }) => {
                    this.errorEvent.emit({
                        message: error,
                        errorCode: code,
                        severity: severity,
                    });
                },
                final: () => (this.isLoading = false),
            });
        };
        this.sendData = () => {
            this.isLoading = true;
            return new Promise((resolve) => {
                if (this.ownerId) {
                    this.patchOwner({
                        payload: this.payload,
                        onSuccess: (response) => {
                            this.ownerSubmitted.emit({ id: response.data.id });
                            this.instantiateOwner(response.data);
                            resolve(true);
                        },
                        onError: ({ error, code, severity }) => {
                            this.errorEvent.emit({
                                message: error,
                                errorCode: code,
                                severity: severity,
                            });
                            resolve(false);
                        },
                        final: () => (this.isLoading = false),
                    });
                }
                else {
                    this.postOwner({
                        payload: this.payload,
                        onSuccess: (response) => {
                            this.ownerSubmitted.emit({ id: response.data.id });
                            this.instantiateOwner(response.data);
                            resolve(true);
                        },
                        onError: ({ error, code, severity }) => {
                            this.errorEvent.emit({
                                message: error,
                                errorCode: code,
                                severity: severity,
                            });
                            resolve(false);
                        },
                        final: () => (this.isLoading = false),
                    });
                }
            });
        };
        this.instantiateOwner = async (data) => {
            this.owner = Object.assign({}, new Owner(data));
            await this.formController.setInitialValues(this.owner);
        };
        this.handleAddOwner = () => {
            const eventName = this.ownerId ? BusinessFormClickActions.updateOwner : BusinessFormClickActions.addOwner;
            this.clickEvent.emit({ name: eventName });
        };
        this.handleRemoveOwner = () => {
            this.removeOwner(this.ownerId);
            this.clickEvent.emit({ name: BusinessFormClickActions.removeOwner });
        };
        this.validate = this.validate.bind(this);
        this.submit = this.submit.bind(this);
        this.validateAndSubmit = this.validateAndSubmit.bind(this);
    }
    async returnOwnerData() {
        return this.ownerId;
    }
    async getOwnershipPercentage() {
        var _a;
        return ((_a = this.formController.values.getValue()) === null || _a === void 0 ? void 0 : _a.ownership_percentage) || null;
    }
    async setOwnershipPercentageError(message) {
        this.formController.setFieldError('ownership_percentage', message);
    }
    async validate() {
        return this.formController.validate();
    }
    async submit() {
        const isValid = await this.validate();
        if (isValid) {
            return this.sendData();
        }
    }
    async validateAndSubmit(event) {
        event.preventDefault();
        const isValid = await this.validate();
        if (isValid) {
            this.submit();
        }
    }
    get payload() {
        const formValues = new Owner(this.formController.values.getValue()).payload;
        if (this.ownerId) {
            return formValues;
        }
        else {
            return Object.assign(Object.assign({}, formValues), { business_id: this.businessId });
        }
    }
    get formTitle() {
        return this.ownerId ? 'Edit Business Owner' : 'Add Business Owner';
    }
    get submitButtonText() {
        return this.ownerId ? 'Update' : 'Add';
    }
    get showRemoveButton() {
        const blankForm = !this.ownerId && this.newFormOpen;
        if (this.ownersLength <= 1) {
            return false;
        }
        else {
            if (blankForm) {
                return true;
            }
            else if (this.ownerId && !this.newFormOpen) {
                return true;
            }
        }
    }
    componentWillLoad() {
        const schemaFactory = identitySchemaByCountry[this.country];
        this.formController = new FormController(schemaFactory('owner', this.allowOptionalFields));
        this.initializeApi();
        this.ownerId && this.getOwner ? this.getData() : this.instantiateOwner({});
    }
    componentDidLoad() {
        this.formController.errors.subscribe((errors) => (this.errors = Object.assign({}, errors)));
    }
    initializeApi() {
        if (!this.authToken) {
            return;
        }
        this.getOwner = makeGetIdentity({
            authToken: this.authToken,
            identityId: this.ownerId,
            service: new IdentityService(),
        });
        this.patchOwner = makePatchIdentity({
            authToken: this.authToken,
            identityId: this.ownerId,
            service: new IdentityService(),
        });
        this.postOwner = makePostIdentity({
            authToken: this.authToken,
            service: new IdentityService(),
        });
    }
    render() {
        return (h(Host, { key: 'b168e9f5a52416984e6a04ab5f0771b63ee28b2e', style: { display: 'block' } }, h("form", { key: 'd22a6aae3f5debd30d87b3bd37c67390a46c4590', onSubmit: this.validateAndSubmit }, h("fieldset", { key: '30597bc1f4983bddbdc0ed45e6be7e715d0b9b8e' }, h("legend", { key: '055ca2b6bbfa4e86591411668eaead3bd0d95ea8', class: "fw-semibold fs-5 w-100", part: heading3 }, this.formTitle), h("br", { key: 'f0d2d9624611ee888ff318ec1525281bf007e346' }), h("div", { key: '2c38a166f681404c2ab303630217e54c15f1e5f2', class: "row gy-3" }, h("owner-form-inputs", { key: '05253624d3d79f0ba7a4ec49b387e252e796e5b6', ownerDefaultValue: this.formController.getInitialValues(), errors: this.errors, formController: this.formController, country: this.country }), h("div", { key: '8e0e67966f047c3f9a4b30f28983f67db2372c0c', class: "d-flex gap-2 justify-content-start" }, h(Button, { key: 'e33368a968efe5faf52a08e36bb2bef215625c4a', variant: "secondary", type: "button", onClick: this.handleRemoveOwner, hidden: !this.showRemoveButton }, "Remove"), h(Button, { key: '7bb79b0624dfb3a8645182432c484f288cc73692', variant: "primary", type: "submit", onClick: this.handleAddOwner, disabled: this.isLoading }, this.submitButtonText))), h("hr", { key: 'ef4f86dd1b1878e92c98e4a826f9fbad808e15b7' })))));
    }
    static get watchers() { return {
        "isLoading": ["loadingWatcher"]
    }; }
}, [256, "owner-form", {
        "authToken": [1, "auth-token"],
        "ownerId": [1, "owner-id"],
        "businessId": [1, "business-id"],
        "allowOptionalFields": [4, "allow-optional-fields"],
        "removeOwner": [16],
        "newFormOpen": [4, "new-form-open"],
        "ownersLength": [2, "owners-length"],
        "country": [1],
        "isLoading": [32],
        "formController": [32],
        "errors": [32],
        "owner": [32],
        "getOwner": [32],
        "patchOwner": [32],
        "postOwner": [32],
        "returnOwnerData": [64],
        "getOwnershipPercentage": [64],
        "setOwnershipPercentageError": [64],
        "validate": [64],
        "submit": [64],
        "validateAndSubmit": [64]
    }, undefined, {
        "isLoading": ["loadingWatcher"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["owner-form", "custom-popper", "form-address-fields", "form-control-date", "form-control-number", "form-control-number-masked", "form-control-select", "form-control-text", "form-control-tooltip", "identity-address-form", "owner-form-inputs"];
    components.forEach(tagName => { switch (tagName) {
        case "owner-form":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, OwnerForm);
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "form-address-fields":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "form-control-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "form-control-number":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "form-control-number-masked":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "form-control-select":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "identity-address-form":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "owner-form-inputs":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { OwnerForm as O, defineCustomElement as d };
