import { h } from "@stencil/core";
import { ComponentErrorCodes, ComponentErrorSeverity } from "../../api/ComponentError";
import JustifiAnalytics from "../../api/Analytics";
import { checkPkgVersion } from "../../utils/check-pkg-version";
import { makeGetDispute } from "../../actions/dispute/dispute-actions";
import { DisputeService } from "../../api/services/dispute.service";
import { DisputeManagementClickActions } from "./event-types";
import { Dispute, DisputeStatus } from "../../api/Dispute";
import { StyledHost } from "../../ui-components";
export class JustifiDisputeManagement {
    constructor() {
        this.isLoading = true;
        this.showDisputeResponseForm = false;
        this.fetchDisputePending = false;
    }
    scheduleFetchDispute() {
        if (!this.getDispute || this.fetchDisputePending) {
            return;
        }
        this.fetchDisputePending = true;
        queueMicrotask(() => {
            this.fetchDisputePending = false;
            if (this.getDispute) {
                this.fetchData();
            }
        });
    }
    disputeResponseHandler(event) {
        if (event.detail.name === DisputeManagementClickActions.respondToDispute) {
            this.showDisputeResponseForm = true;
        }
        if (event.detail.name === DisputeManagementClickActions.cancelDispute) {
            this.showDisputeResponseForm = false;
        }
    }
    disputeSubmittedHandler() {
        this.fetchData();
    }
    componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
        this.initializeGetDispute();
    }
    componentDidLoad() {
        this.scheduleFetchDispute();
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    propChanged() {
        this.initializeGetDispute();
        this.scheduleFetchDispute();
    }
    initializeGetDispute() {
        if (this.disputeId && this.authToken) {
            this.getDispute = makeGetDispute({
                id: this.disputeId,
                authToken: this.authToken,
                service: new DisputeService(),
            });
        }
        else {
            this.errorEvent.emit({
                message: 'Dispute ID and Auth Token are required',
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    fetchData() {
        if (!this.getDispute) {
            return;
        }
        this.isLoading = true;
        this.getDispute({
            onSuccess: ({ dispute }) => {
                this.dispute = new Dispute(dispute);
                this.isLoading = false;
                if (this.dispute.status !== DisputeStatus.needsResponse) {
                    this.showDisputeResponseForm = false;
                }
            },
            onError: ({ error, code, severity }) => {
                this.errorEvent.emit({
                    errorCode: code,
                    message: error,
                    severity,
                });
                this.isLoading = false;
            },
        });
    }
    render() {
        return (h(StyledHost, { key: 'e95c2e42f6e966681986089eccd9c0c6099d7214' }, h("div", { key: '18e936c7fd6992cfc18f8e98a2eda2f333c862f2' }, this.showDisputeResponseForm ? (h("dispute-response", { disputeId: this.disputeId, disputeResponse: this.dispute.dispute_response, authToken: this.authToken })) : (h("dispute-notification", { dispute: this.dispute, authToken: this.authToken, isLoading: this.isLoading })))));
    }
    static get is() { return "justifi-dispute-management"; }
    static get encapsulation() { return "shadow"; }
    static get properties() {
        return {
            "disputeId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "dispute-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 {
            "getDispute": {},
            "dispute": {},
            "isLoading": {},
            "showDisputeResponseForm": {}
        };
    }
    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": "disputeId",
                "methodName": "propChanged"
            }, {
                "propName": "authToken",
                "methodName": "propChanged"
            }];
    }
    static get listeners() {
        return [{
                "name": "click-event",
                "method": "disputeResponseHandler",
                "target": undefined,
                "capture": false,
                "passive": false
            }, {
                "name": "submit-event",
                "method": "disputeSubmittedHandler",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
