import { r as registerInstance, c as createEvent, h } from './index-CV0Pyo1O.js';
import { D as Dispute, C as ComponentErrorSeverity, b as ComponentErrorCodes, h as DisputeStatus } from './Plaid-DxSkT8pY.js';
import { J as JustifiAnalytics } from './Analytics-C76Y5Pqf.js';
import { c as checkPkgVersion } from './check-pkg-version-D5BxTNyL.js';
import { g as getErrorCode, a as getErrorMessage } from './utils-CVYvs2hZ.js';
import { D as DisputeManagementClickActions, a as DisputeService } from './event-types-oyUZYVGh.js';
import { S as StyledHost } from './styled-host-D84RBv1W.js';
import './event-types-CZfYo5-1.js';
import './utils-C25uIOK0.js';
import './dinero-HXpYMWUU.js';
import './package-CqcLgdsl.js';
import './Api-BulOmiMS.js';
import './config-state-kKn89dp1.js';
import './index-cDVGjRkU.js';
import './Pagination-CLHwhyvu.js';
import './Business-BcNb9bVO.js';

const makeGetDispute = ({ id, authToken, service }) => async ({ onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.fetchDispute(id, authToken);
        if (!response.error) {
            const pagingInfo = Object.assign({}, response.page_info);
            const dispute = new Dispute(response.data);
            onSuccess({ dispute, pagingInfo });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            return onError({
                error: responseError,
                code,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: ComponentErrorSeverity.ERROR,
        });
    }
};

const JustifiDisputeManagement = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.errorEvent = createEvent(this, "error-event", 7);
        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 watchers() { return {
        "disputeId": ["propChanged"],
        "authToken": ["propChanged"]
    }; }
};

export { JustifiDisputeManagement as justifi_dispute_management };
