import { h } from "@stencil/core";
import { ErrorState } from "../../ui-components/details/utils";
import { BusinessService } from "../../api/services/business.service";
import { makeGetBusiness } from "../../actions/business/get-business";
import { ComponentErrorCodes, ComponentErrorSeverity } from "../../api/ComponentError";
import JustifiAnalytics from "../../api/Analytics";
import { checkPkgVersion } from "../../utils/check-pkg-version";
import { StyledHost } from "../../ui-components";
import { BusinessDetailsLoading } from "./business-details-loading";
export class JustifiBusinessDetails {
    constructor() {
        this.loading = true;
    }
    componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
        this.initializeGetBusiness();
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    ;
    propChanged() {
        this.initializeGetBusiness();
    }
    initializeGetBusiness() {
        if (this.businessId && this.authToken) {
            this.getBusiness = makeGetBusiness({
                id: this.businessId,
                authToken: this.authToken,
                service: new BusinessService(),
            });
            this.fetchData();
        }
        else {
            this.errorMessage = 'Invalid business id or auth token';
            this.errorEvent.emit({
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                message: this.errorMessage,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    updateOnPropChange() {
        if (this.getBusiness) {
            this.fetchData();
        }
    }
    fetchData() {
        this.loading = true;
        this.getBusiness({
            onSuccess: ({ business }) => {
                this.business = business;
                this.errorMessage = null;
            },
            onError: ({ error, code, severity }) => {
                this.errorMessage = error;
                this.errorEvent.emit({
                    errorCode: code,
                    message: error,
                    severity,
                });
            },
            final: () => {
                this.loading = false;
            },
        });
    }
    render() {
        return (h(StyledHost, { key: '7ce48e6905cdbbf8f94470913ecfa3ef8dbb76c7' }, this.loading && h(BusinessDetailsLoading, { key: '7c3439e0c247436cd7951d3bdc32e7672c9f8447' }), !this.loading && this.errorMessage && ErrorState(this.errorMessage), !this.loading && !this.errorMessage && this.business && (h("justifi-details", { key: 'bf57dacf2067f2704014ce3e002958e3306bba73', errorMessage: this.errorMessage }, h("div", { key: '46d19a178ba0d33aa21a4f3900128e3e905731a9', slot: 'detail-sections' }, h("core-info-details", { key: 'ac217a4cfe6b45810a85ca320760b7331f76e60b', business: this.business }), h("legal-address-details", { key: '1fc1a196a43bcf5f9eafd09bfe74c8bb7c409797', legalAddress: this.business.legal_address }), h("representative-details", { key: '1e0833606b129f69a05946726172963cbec05189', representative: this.business.representative }), h("owner-details", { key: 'c974111dc67a8961ef6435c34843ca927ad8a7e5', owners: this.business.owners }), h("additional-questions-details", { key: '0951d284a373457e98a1dafc80231ec3ee057e0a', additionalQuestions: this.business.additional_questions }))))));
    }
    static get is() { return "justifi-business-details"; }
    static get encapsulation() { return "shadow"; }
    static get properties() {
        return {
            "businessId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "business-id"
            },
            "authToken": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "auth-token"
            }
        };
    }
    static get states() {
        return {
            "getBusiness": {},
            "business": {},
            "loading": {},
            "errorMessage": {}
        };
    }
    static get events() {
        return [{
                "method": "errorEvent",
                "name": "error-event",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "ComponentErrorEvent",
                    "resolved": "ComponentErrorEvent",
                    "references": {
                        "ComponentErrorEvent": {
                            "location": "import",
                            "path": "../../api/ComponentEvents",
                            "id": "src/api/ComponentEvents.ts::ComponentErrorEvent"
                        }
                    }
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "businessId",
                "methodName": "propChanged"
            }, {
                "propName": "authToken",
                "methodName": "propChanged"
            }, {
                "propName": "getBusiness",
                "methodName": "updateOnPropChange"
            }];
    }
}
