import { h, proxyCustomElement, HTMLElement, createEvent } from '@stencil/core/internal/client';
import { P as PayoutService } from './payout.service.js';
import './config-state.js';
import './dinero.js';
import { o as Payout, C as ComponentErrorSeverity, a as ComponentErrorCodes } from './Plaid.js';
import './Pagination.js';
import './Business.js';
import './ApplePay.js';
import { g as getErrorCode, a as getErrorMessage } from './utils3.js';
import { j as capitalizeFirstLetter, f as formatDate, a as formatTime } from './utils2.js';
import { E as ErrorState, b as EntityHeadInfo, c as EntityHeadInfoItem, D as DetailSectionTitle, a as DetailItem, C as CodeBlock } from './utils.js';
import { J as JustifiAnalytics } from './Analytics.js';
import { m as makeGetPayoutCSV, M as MapPayoutStatusToBadge } from './payouts-status.js';
import { c as checkPkgVersion } from './check-pkg-version.js';
import { B as Button } from './button.js';
import { S as StyledHost } from './styled-host.js';
import { d as defineCustomElement$2 } from './justifi-details2.js';
import './event-types.js';
import './justifi-radio-list-item2.js';
import { S as Skeleton } from './skeleton.js';
import { B as Badge, a as BadgeVariant } from './badge.js';

const makeGetPayoutDetails = ({ id, authToken, service }) => async ({ onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.fetchPayout(id, authToken);
        if (!response.error) {
            const payout = new Payout(response.data);
            onSuccess(payout);
        }
        else {
            const errorMessage = getErrorMessage(response.error);
            const code = getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            onError({
                error: errorMessage,
                code,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const errorMessage = getErrorMessage(error);
        const code = getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        onError({
            error: errorMessage,
            code,
            severity: ComponentErrorSeverity.ERROR,
        });
    }
};

const PayoutDetailsLoading = () => (h("div", { class: "row gap-3" }, h("div", { class: "col-12" }, h(Skeleton, { width: "35%", height: "30px" })), h("div", { class: "col-12" }, h(Skeleton, { width: "70%" })), h("div", { class: "col-12" }, h(Skeleton, { width: "40%" })), h("div", { class: "col-12" }, h(Skeleton, { width: "40%" })), h("div", { class: "col-12" }, h(Skeleton, { width: "100%" })), h("div", { class: "col-12" }, h(Skeleton, { width: "80%" }))));

const JustifiPayoutDetails$1 = /*@__PURE__*/ proxyCustomElement(class JustifiPayoutDetails extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.__attachShadow();
        this.errorEvent = createEvent(this, "error-event", 7);
        this.recordClickEvent = createEvent(this, "record-click-event", 7);
        this.enableRecordClick = false;
        this.loading = true;
        this.errorMessage = null;
        this.downloadCSV = () => {
            this.getPayoutCSV({
                payoutId: this.payout.id,
                onError: ({ error, code, severity }) => {
                    this.errorMessage = error;
                    this.errorEvent.emit({
                        message: error,
                        errorCode: code,
                        severity,
                    });
                },
            });
        };
        this.handleRecordClick = (id) => {
            this.recordClickEvent.emit({
                id,
                type: 'account'
            });
        };
    }
    componentWillLoad() {
        checkPkgVersion();
        this.analytics = new JustifiAnalytics(this);
        this.initializeServices();
        if (this.getPayout) {
            this.fetchData();
        }
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    ;
    propChanged() {
        this.initializeServices();
    }
    getPayoutWatcher() {
        this.fetchData();
    }
    initializeServices() {
        if (this.payoutId && this.authToken) {
            this.getPayout = makeGetPayoutDetails({
                id: this.payoutId,
                authToken: this.authToken,
                service: new PayoutService()
            });
            this.getPayoutCSV = makeGetPayoutCSV({
                authToken: this.authToken,
                service: new PayoutService()
            });
        }
        else {
            this.errorMessage = 'Failed to load payout details. payoutId or authToken is not provided.';
            this.errorEvent.emit({
                message: this.errorMessage,
                errorCode: ComponentErrorCodes.MISSING_PROPS,
                severity: ComponentErrorSeverity.ERROR
            });
        }
    }
    fetchData() {
        this.loading = true;
        this.getPayout({
            onSuccess: (payout) => {
                this.errorMessage = null;
                this.payout = payout;
                this.loading = false;
            },
            onError: ({ error, code, severity }) => {
                this.errorMessage = error;
                this.errorEvent.emit({
                    message: error,
                    errorCode: code,
                    severity,
                });
                this.loading = false;
            },
        });
    }
    formatMethod(deliveryMethod) {
        if (deliveryMethod === "ach")
            return "ACH";
        return capitalizeFirstLetter(deliveryMethod);
    }
    render() {
        var _a, _b, _c, _d;
        return (h(StyledHost, { key: 'da86ceeb41265056f9bc64c07b3bd7db4a91471c' }, this.loading && h(PayoutDetailsLoading, { key: '49ae2848db4a5b59770c2b84bfba745861573a6d' }), !this.loading && this.errorMessage && ErrorState(this.errorMessage), !this.loading && this.payout && (h("justifi-details", { key: 'b4e6740d49ae1099f28ea3d6f21fe0da48149c71', errorMessage: this.errorMessage }, h(EntityHeadInfo, { key: 'e9efbeeb7894996f318d90fb38826a6a9e4cc4b3', slot: "head-info", badge: (h("div", { class: "d-flex gap-1" }, MapPayoutStatusToBadge((_a = this.payout) === null || _a === void 0 ? void 0 : _a.status), this.payout.settlement_priority === 'expedited' ? (h(Badge, { variant: BadgeVariant.WARNING, title: "Expedited", text: "Expedited" })) : (h(Badge, { variant: BadgeVariant.SECONDARY, title: "Standard", text: "Standard" })))), title: this.payout.formattedPaymentAmount(this.payout.amount) }, h(EntityHeadInfoItem, { key: 'feb913d2b90e86fb4f5575379e9b9a47adfa723f', classes: "border-1 border-end", title: "Updated At", value: `${formatDate(this.payout.updated_at)} ${formatTime(this.payout.updated_at)}` }), h(EntityHeadInfoItem, { key: '9fe6da08323735948f145a72a02db26a6eb31f02', classes: "border-1 border-end", title: "Created At", value: `${formatDate(this.payout.created_at)} ${formatTime(this.payout.created_at)}` }), h(EntityHeadInfoItem, { key: 'd01c4b7ea7c966b524026a6e97c0575466de3f74', title: "ID", value: this.payout.id }), h("div", { key: '4c4f73e5898caedbb1b7aba326dbfb0dcfbb59e6', class: "m-4" }, h(Button, { key: '622a6e8384da37f4bb3896bcafea46ef697b140e', variant: "outline-secondary", class: "btn btn-outline-secondary d-flex align-items-center", onClick: this.downloadCSV }, "Export CSV"))), h("div", { key: '9b9271663673dc2e78e89bec5c7c9c47daa6761e', slot: 'detail-sections' }, h(DetailSectionTitle, { key: 'ac5c6e1c09e63cd0616794b7df367a73889e37b2', sectionTitle: "Details" }), h("div", { key: '95f9d5a0097499fdb24f44ea87313c3b34a96068', class: "d-flex flex-column gap-2 w-100" }, h(DetailItem, { key: 'ecf60d4a4783c8fb52e10222dd4f6e9935cd0910', title: "Date paid", value: formatDate(this.payout.deposits_at) }), h(DetailItem, { key: '289971a08641bc2dc725d603ef611291025cd6b4', title: "Statement Description", value: this.payout.description }), h(DetailItem, { key: 'd237aacbeb9b23ac0d45aa978350aad4824b0695', title: "Payout Method", value: this.formatMethod(this.payout.delivery_method) }), h(DetailItem, { key: '744ef2b8878b742483750fb921ca9790f6aa9ec1', title: "Amount", value: this.payout.formattedPaymentAmount(this.payout.amount) }), h(DetailItem, { key: 'dbf3327510523afc6edd78ea2926f99593da9748', title: "Fee", value: this.payout.formattedPaymentAmount(this.payout.fees_total) })), h(DetailSectionTitle, { key: '15983342dbe916770291f33226b744e8c4c1129e', sectionTitle: "Account" }), h("div", { key: 'b3fb4353794dc490633a14abcc829ff0835d989f', class: "d-flex flex-column gap-2 w-100" }, h(DetailItem, { key: '665fbe47e3b35c0b7ffb3e6c17e15c3703c90250', title: "ID", value: this.payout.account_id, onClick: this.enableRecordClick ? () => this.handleRecordClick(this.payout.account_id) : undefined }), h(DetailItem, { key: '5b15d5bc8a63541ac8d77eddccb47c89659fd05a', title: "Account Type", value: (_b = this.payout.bank_account) === null || _b === void 0 ? void 0 : _b.account_type }), h(DetailItem, { key: '947043e3571f27a672060f2101296daba0073727', title: "Institution", value: (_c = this.payout.bank_account) === null || _c === void 0 ? void 0 : _c.account_type }), h(DetailItem, { key: '74ac687b37c7ca91a88863730560333c41b9413c', title: "Routing Number", value: (_d = this.payout.bank_account) === null || _d === void 0 ? void 0 : _d.routing_number }), h(DetailItem, { key: '43cd9aa5ac45f6efba97901e3e1386446065c28a', title: "Account Number", value: this.payout.bank_account ? `**** ${this.payout.bank_account.account_number_last4}` : undefined })), this.payout.metadata && [
            h(DetailSectionTitle, { key: '8bf04ae43a08d8ef624357f64df900a11b8f2dad', sectionTitle: 'Metadata' }),
            h("div", { key: '1bf737734f34f626cbfb9dd878afbb91b6f01913', class: "d-table gap-2 w-100" }, h(CodeBlock, { key: '0d1481e1823e9b2f28328ebfb3cd1c92c6342cc6', metadata: this.payout.metadata }))
        ])))));
    }
    static get watchers() { return {
        "payoutId": ["propChanged"],
        "authToken": ["propChanged"],
        "getPayout": ["getPayoutWatcher"]
    }; }
}, [257, "justifi-payout-details", {
        "payoutId": [1, "payout-id"],
        "authToken": [1, "auth-token"],
        "enableRecordClick": [4, "enable-record-click"],
        "getPayout": [32],
        "getPayoutCSV": [32],
        "payout": [32],
        "loading": [32],
        "errorMessage": [32]
    }, undefined, {
        "payoutId": ["propChanged"],
        "authToken": ["propChanged"],
        "getPayout": ["getPayoutWatcher"]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["justifi-payout-details", "justifi-details"];
    components.forEach(tagName => { switch (tagName) {
        case "justifi-payout-details":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, JustifiPayoutDetails$1);
            }
            break;
        case "justifi-details":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const JustifiPayoutDetails = JustifiPayoutDetails$1;
const defineCustomElement = defineCustomElement$1;

export { JustifiPayoutDetails, defineCustomElement };
