'use strict';

var index = require('./index-p7cvUYAE.js');
var Plaid = require('./Plaid-CPESyiiV.js');
var Analytics = require('./Analytics-C6KBHEIP.js');
var checkPkgVersion = require('./check-pkg-version-B9EoYyZR.js');
var utils = require('./utils-BsnepLy6.js');
var eventTypes = require('./event-types-VgI-nT8p.js');
var styledHost = require('./styled-host-Ch0-dxF-.js');
require('./event-types-YOY5TKUN.js');
require('./utils-CYONRFR6.js');
require('./dinero-BwNN_k7x.js');
require('./package-YyJzVqrh.js');
require('./Api-D9vOcUw4.js');
require('./config-state-BIgGhhe9.js');
require('./index-W-zoNiyM.js');
require('./Pagination-Dediz133.js');
require('./Business-oBifyXkW.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 Plaid.Dispute(response.data);
            onSuccess({ dispute, pagingInfo });
        }
        else {
            const responseError = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            return onError({
                error: responseError,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
};

const JustifiDisputeManagement = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.errorEvent = index.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 === eventTypes.DisputeManagementClickActions.respondToDispute) {
            this.showDisputeResponseForm = true;
        }
        if (event.detail.name === eventTypes.DisputeManagementClickActions.cancelDispute) {
            this.showDisputeResponseForm = false;
        }
    }
    disputeSubmittedHandler() {
        this.fetchData();
    }
    componentWillLoad() {
        checkPkgVersion.checkPkgVersion();
        this.analytics = new Analytics.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 eventTypes.DisputeService(),
            });
        }
        else {
            this.errorEvent.emit({
                message: 'Dispute ID and Auth Token are required',
                errorCode: Plaid.ComponentErrorCodes.MISSING_PROPS,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    fetchData() {
        if (!this.getDispute) {
            return;
        }
        this.isLoading = true;
        this.getDispute({
            onSuccess: ({ dispute }) => {
                this.dispute = new Plaid.Dispute(dispute);
                this.isLoading = false;
                if (this.dispute.status !== Plaid.DisputeStatus.needsResponse) {
                    this.showDisputeResponseForm = false;
                }
            },
            onError: ({ error, code, severity }) => {
                this.errorEvent.emit({
                    errorCode: code,
                    message: error,
                    severity,
                });
                this.isLoading = false;
            },
        });
    }
    render() {
        return (index.h(styledHost.StyledHost, { key: 'e95c2e42f6e966681986089eccd9c0c6099d7214' }, index.h("div", { key: '18e936c7fd6992cfc18f8e98a2eda2f333c862f2' }, this.showDisputeResponseForm ? (index.h("dispute-response", { disputeId: this.disputeId, disputeResponse: this.dispute.dispute_response, authToken: this.authToken })) : (index.h("dispute-notification", { dispute: this.dispute, authToken: this.authToken, isLoading: this.isLoading })))));
    }
    static get watchers() { return {
        "disputeId": ["propChanged"],
        "authToken": ["propChanged"]
    }; }
};

exports.justifi_dispute_management = JustifiDisputeManagement;
