import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { D as Dispute, C as ComponentErrorSeverity, a as ComponentErrorCodes, l as DisputeStatus } from './Plaid.js';
import { J as JustifiAnalytics } from './Analytics.js';
import { c as checkPkgVersion } from './check-pkg-version.js';
import { g as getErrorCode, a as getErrorMessage } from './utils3.js';
import { D as DisputeManagementClickActions, a as DisputeService } from './event-types3.js';
import { S as StyledHost } from './styled-host.js';
import './justifi-details2.js';
import './event-types.js';
import './justifi-radio-list-item2.js';
import { d as defineCustomElement$i } from './additional-statement2.js';
import { d as defineCustomElement$h } from './cancellation-policy2.js';
import { d as defineCustomElement$g } from './custom-popper2.js';
import { d as defineCustomElement$f } from './customer-details2.js';
import { d as defineCustomElement$e } from './dispute-notification2.js';
import { d as defineCustomElement$d } from './dispute-response2.js';
import { d as defineCustomElement$c } from './duplicate-charge2.js';
import { d as defineCustomElement$b } from './electronic-evidence2.js';
import { d as defineCustomElement$a } from './form-control-date2.js';
import { d as defineCustomElement$9 } from './form-control-file-v22.js';
import { d as defineCustomElement$8 } from './form-control-help-text3.js';
import { d as defineCustomElement$7 } from './form-control-text2.js';
import { d as defineCustomElement$6 } from './form-control-textarea2.js';
import { d as defineCustomElement$5 } from './form-control-tooltip2.js';
import { d as defineCustomElement$4 } from './product-or-service2.js';
import { d as defineCustomElement$3 } from './refund-policy2.js';
import { d as defineCustomElement$2 } from './shipping-details2.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$1 = /*@__PURE__*/ proxyCustomElement(class JustifiDisputeManagement extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        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"]
    }; }
}, [257, "justifi-dispute-management", {
        "disputeId": [1, "dispute-id"],
        "authToken": [1, "auth-token"],
        "getDispute": [32],
        "dispute": [32],
        "isLoading": [32],
        "showDisputeResponseForm": [32]
    }, [[0, "click-event", "disputeResponseHandler"], [0, "submit-event", "disputeSubmittedHandler"]], {
        "disputeId": ["propChanged"],
        "authToken": ["propChanged"]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["justifi-dispute-management", "additional-statement", "cancellation-policy", "custom-popper", "customer-details", "dispute-notification", "dispute-response", "duplicate-charge", "electronic-evidence", "form-control-date", "form-control-file-v2", "form-control-help-text", "form-control-text", "form-control-textarea", "form-control-tooltip", "product-or-service", "refund-policy", "shipping-details"];
    components.forEach(tagName => { switch (tagName) {
        case "justifi-dispute-management":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, JustifiDisputeManagement$1);
            }
            break;
        case "additional-statement":
            if (!customElements.get(tagName)) {
                defineCustomElement$i();
            }
            break;
        case "cancellation-policy":
            if (!customElements.get(tagName)) {
                defineCustomElement$h();
            }
            break;
        case "custom-popper":
            if (!customElements.get(tagName)) {
                defineCustomElement$g();
            }
            break;
        case "customer-details":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "dispute-notification":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "dispute-response":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "duplicate-charge":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "electronic-evidence":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "form-control-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "form-control-file-v2":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "form-control-help-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "form-control-text":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "form-control-textarea":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "form-control-tooltip":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "product-or-service":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "refund-policy":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "shipping-details":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const JustifiDisputeManagement = JustifiDisputeManagement$1;
const defineCustomElement = defineCustomElement$1;

export { JustifiDisputeManagement, defineCustomElement };
