'use strict';

var index = require('./index-p7cvUYAE.js');
var payout_service = require('./payout.service-B6j2g-G5.js');
require('./config-state-BIgGhhe9.js');
require('./dinero-BwNN_k7x.js');
var Plaid = require('./Plaid-CPESyiiV.js');
require('./Pagination-Dediz133.js');
require('./Business-oBifyXkW.js');
var utils = require('./utils-BsnepLy6.js');
var utils$1 = require('./utils-CYONRFR6.js');
var utils$2 = require('./utils-8RGQVADu.js');
var Analytics = require('./Analytics-C6KBHEIP.js');
var payoutsStatus = require('./payouts-status-C2KFg1AD.js');
var checkPkgVersion = require('./check-pkg-version-B9EoYyZR.js');
var button = require('./button-CR9Rl1-o.js');
var styledHost = require('./styled-host-Ch0-dxF-.js');
require('./event-types-YOY5TKUN.js');
var skeleton = require('./skeleton-DvRCiQlA.js');
var badge = require('./badge-GBNtUypt.js');
require('./Api-D9vOcUw4.js');
require('./index-W-zoNiyM.js');
require('./parts-BXshb_S-.js');
require('./package-YyJzVqrh.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 Plaid.Payout(response.data);
            onSuccess(payout);
        }
        else {
            const errorMessage = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            onError({
                error: errorMessage,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const errorMessage = utils.getErrorMessage(error);
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        onError({
            error: errorMessage,
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
};

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

const JustifiPayoutDetails = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.errorEvent = index.createEvent(this, "error-event", 7);
        this.recordClickEvent = index.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.checkPkgVersion();
        this.analytics = new Analytics.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 payout_service.PayoutService()
            });
            this.getPayoutCSV = payoutsStatus.makeGetPayoutCSV({
                authToken: this.authToken,
                service: new payout_service.PayoutService()
            });
        }
        else {
            this.errorMessage = 'Failed to load payout details. payoutId or authToken is not provided.';
            this.errorEvent.emit({
                message: this.errorMessage,
                errorCode: Plaid.ComponentErrorCodes.MISSING_PROPS,
                severity: Plaid.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 utils$1.capitalizeFirstLetter(deliveryMethod);
    }
    render() {
        var _a, _b, _c, _d;
        return (index.h(styledHost.StyledHost, { key: 'da86ceeb41265056f9bc64c07b3bd7db4a91471c' }, this.loading && index.h(PayoutDetailsLoading, { key: '49ae2848db4a5b59770c2b84bfba745861573a6d' }), !this.loading && this.errorMessage && utils$2.ErrorState(this.errorMessage), !this.loading && this.payout && (index.h("justifi-details", { key: 'b4e6740d49ae1099f28ea3d6f21fe0da48149c71', errorMessage: this.errorMessage }, index.h(utils$2.EntityHeadInfo, { key: 'e9efbeeb7894996f318d90fb38826a6a9e4cc4b3', slot: "head-info", badge: (index.h("div", { class: "d-flex gap-1" }, payoutsStatus.MapPayoutStatusToBadge((_a = this.payout) === null || _a === void 0 ? void 0 : _a.status), this.payout.settlement_priority === 'expedited' ? (index.h(badge.Badge, { variant: badge.BadgeVariant.WARNING, title: "Expedited", text: "Expedited" })) : (index.h(badge.Badge, { variant: badge.BadgeVariant.SECONDARY, title: "Standard", text: "Standard" })))), title: this.payout.formattedPaymentAmount(this.payout.amount) }, index.h(utils$2.EntityHeadInfoItem, { key: 'feb913d2b90e86fb4f5575379e9b9a47adfa723f', classes: "border-1 border-end", title: "Updated At", value: `${utils$1.formatDate(this.payout.updated_at)} ${utils$1.formatTime(this.payout.updated_at)}` }), index.h(utils$2.EntityHeadInfoItem, { key: '9fe6da08323735948f145a72a02db26a6eb31f02', classes: "border-1 border-end", title: "Created At", value: `${utils$1.formatDate(this.payout.created_at)} ${utils$1.formatTime(this.payout.created_at)}` }), index.h(utils$2.EntityHeadInfoItem, { key: 'd01c4b7ea7c966b524026a6e97c0575466de3f74', title: "ID", value: this.payout.id }), index.h("div", { key: '4c4f73e5898caedbb1b7aba326dbfb0dcfbb59e6', class: "m-4" }, index.h(button.Button, { key: '622a6e8384da37f4bb3896bcafea46ef697b140e', variant: "outline-secondary", class: "btn btn-outline-secondary d-flex align-items-center", onClick: this.downloadCSV }, "Export CSV"))), index.h("div", { key: '9b9271663673dc2e78e89bec5c7c9c47daa6761e', slot: 'detail-sections' }, index.h(utils$2.DetailSectionTitle, { key: 'ac5c6e1c09e63cd0616794b7df367a73889e37b2', sectionTitle: "Details" }), index.h("div", { key: '95f9d5a0097499fdb24f44ea87313c3b34a96068', class: "d-flex flex-column gap-2 w-100" }, index.h(utils$2.DetailItem, { key: 'ecf60d4a4783c8fb52e10222dd4f6e9935cd0910', title: "Date paid", value: utils$1.formatDate(this.payout.deposits_at) }), index.h(utils$2.DetailItem, { key: '289971a08641bc2dc725d603ef611291025cd6b4', title: "Statement Description", value: this.payout.description }), index.h(utils$2.DetailItem, { key: 'd237aacbeb9b23ac0d45aa978350aad4824b0695', title: "Payout Method", value: this.formatMethod(this.payout.delivery_method) }), index.h(utils$2.DetailItem, { key: '744ef2b8878b742483750fb921ca9790f6aa9ec1', title: "Amount", value: this.payout.formattedPaymentAmount(this.payout.amount) }), index.h(utils$2.DetailItem, { key: 'dbf3327510523afc6edd78ea2926f99593da9748', title: "Fee", value: this.payout.formattedPaymentAmount(this.payout.fees_total) })), index.h(utils$2.DetailSectionTitle, { key: '15983342dbe916770291f33226b744e8c4c1129e', sectionTitle: "Account" }), index.h("div", { key: 'b3fb4353794dc490633a14abcc829ff0835d989f', class: "d-flex flex-column gap-2 w-100" }, index.h(utils$2.DetailItem, { key: '665fbe47e3b35c0b7ffb3e6c17e15c3703c90250', title: "ID", value: this.payout.account_id, onClick: this.enableRecordClick ? () => this.handleRecordClick(this.payout.account_id) : undefined }), index.h(utils$2.DetailItem, { key: '5b15d5bc8a63541ac8d77eddccb47c89659fd05a', title: "Account Type", value: (_b = this.payout.bank_account) === null || _b === void 0 ? void 0 : _b.account_type }), index.h(utils$2.DetailItem, { key: '947043e3571f27a672060f2101296daba0073727', title: "Institution", value: (_c = this.payout.bank_account) === null || _c === void 0 ? void 0 : _c.account_type }), index.h(utils$2.DetailItem, { key: '74ac687b37c7ca91a88863730560333c41b9413c', title: "Routing Number", value: (_d = this.payout.bank_account) === null || _d === void 0 ? void 0 : _d.routing_number }), index.h(utils$2.DetailItem, { key: '43cd9aa5ac45f6efba97901e3e1386446065c28a', title: "Account Number", value: this.payout.bank_account ? `**** ${this.payout.bank_account.account_number_last4}` : undefined })), this.payout.metadata && [
            index.h(utils$2.DetailSectionTitle, { key: '8bf04ae43a08d8ef624357f64df900a11b8f2dad', sectionTitle: 'Metadata' }),
            index.h("div", { key: '1bf737734f34f626cbfb9dd878afbb91b6f01913', class: "d-table gap-2 w-100" }, index.h(utils$2.CodeBlock, { key: '0d1481e1823e9b2f28328ebfb3cd1c92c6342cc6', metadata: this.payout.metadata }))
        ])))));
    }
    static get watchers() { return {
        "payoutId": ["propChanged"],
        "authToken": ["propChanged"],
        "getPayout": ["getPayoutWatcher"]
    }; }
};

exports.justifi_payout_details = JustifiPayoutDetails;
