'use strict';

var index = require('./index-p7cvUYAE.js');
var configState = require('./config-state-BIgGhhe9.js');
require('./dinero-BwNN_k7x.js');
var Plaid = require('./Plaid-CPESyiiV.js');
var Pagination = require('./Pagination-Dediz133.js');
require('./Business-oBifyXkW.js');
var Analytics = require('./Analytics-C6KBHEIP.js');
var checkPkgVersion = require('./check-pkg-version-B9EoYyZR.js');
var utils = require('./utils-BsnepLy6.js');
var payout_service = require('./payout.service-B6j2g-G5.js');
var table = require('./table-DjbNv28p.js');
var styledHost = require('./styled-host-Ch0-dxF-.js');
var eventTypes = require('./event-types-YOY5TKUN.js');
var utils$1 = require('./utils-CYONRFR6.js');
var parts = require('./parts-BXshb_S-.js');
require('./index-W-zoNiyM.js');
require('./package-YyJzVqrh.js');
require('./Api-D9vOcUw4.js');
require('./spinner-C6ZGOB2P.js');

const makeGetPayoutTransactions = ({ accountId, authToken, service }) => async ({ params, onSuccess, onError, final }) => {
    var _a;
    try {
        const response = await service.fetchPayoutTransactions(accountId, authToken, params);
        if (!response.error) {
            const balanceTransactions = response.data.map((dataItem) => new Plaid.PayoutBalanceTransaction(dataItem));
            const pagingInfo = Object.assign({}, response.page_info);
            onSuccess({ balanceTransactions, pagingInfo });
        }
        else {
            const responseError = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            onError({
                error: responseError,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        onError({
            error: utils.getErrorMessage(error),
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
    finally {
        final();
    }
};

const defaultColumnsKeys = 'created_at,txn_type,description,amount,fee,net';
const payoutTransactionTableColumns = {
    id: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The unique identifier of each transaction" }, "ID")),
    account_id: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The unique identifier of the account associated with each transaction" }, "Account ID")),
    payout_id: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The unique identifier of the payout associated with each transaction" }, "Payout ID")),
    financial_transaction_id: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The unique identifier of the financial transaction associated with the payment balance transaction" }, "Financial Transaction ID")),
    amount: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The dollar amount of each transaction" }, "Amount")),
    fee: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The fee associated with each transaction" }, "Fee")),
    net: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The net amount of each transaction" }, "Net")),
    currency: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The currency of each transaction" }, "Currency")),
    description: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The description of each transaction" }, "Description")),
    source_id: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The unique identifier of the source object associated with the payment balance transaction" }, "Source ID")),
    txn_type: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The type of each transaction" }, "Type")),
    created_at: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The date and time each transaction was made" }, "Processed On")),
    available_on: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The date and time each transaction will be available" }, "Available On")),
    updated_at: () => (index.h("th", { part: parts.tableHeadCell, scope: "col", title: "The date and time each transaction was last updated" }, "Updated On")),
};
const payoutTransactionTypes = {
    fee: "Fee",
    payment: "Payment",
    payout: "Payout",
    seller_payment_refund: "Seller Payment Refund",
    dispute_amount_collected: "Dispute Amount Collected",
    dispute_fee_collected: "Dispute Fee Collected",
    seller_payment: "Seller Payment",
    transfer: "Transfer",
    partner_platform_proceeds_credit: "Partner Platform Proceeds Credit",
    processing_fee: "Processing Fee",
    refund_processing_fee: "Refund Processing Fee",
    fee_rounding_adjustment: "Fee Rounding Adjustment",
    platform_fee: "Platform Fee",
    processing_fee_return: "Processing Fee Return",
    platform_fee_return: "Platform Fee Return"
};
const payoutTransactionTableCells = {
    id: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.id) }, transaction.id)),
    account_id: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.account_id) }, transaction.account_id)),
    payout_id: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.payout_id) }, transaction.payout_id)),
    financial_transaction_id: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.financial_transaction_id) }, transaction.financial_transaction_id)),
    amount: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.amount) }, transaction.formattedPaymentAmount(transaction.amount))),
    fee: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.fee) }, transaction.formattedPaymentAmount(transaction.fee))),
    net: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.net) }, transaction.formattedPaymentAmount(transaction.net))),
    currency: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.currency) }, transaction.currency)),
    description: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.description) }, transaction.description)),
    source_id: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.source_id) }, transaction.source_id)),
    txn_type: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.txn_type) }, payoutTransactionTypes[transaction.txn_type])),
    created_at: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.created_at) }, index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.created_at, { showDisplayDate: true })), index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.created_at, { showTime: true })))),
    available_on: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.available_on) }, index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.available_on, { showDisplayDate: true })), index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.available_on, { showTime: true })))),
    updated_at: (transaction) => (index.h("td", { part: parts.getAlternateTableCellPart(transaction.updated_at) }, index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.updated_at, { showDisplayDate: true })), index.h("div", { class: "fw-bold" }, utils$1.convertToLocal(transaction.updated_at, { showTime: true })))),
};

const JustifiPayoutTransactionsList = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.clickEvent = index.createEvent(this, "click-event", 7);
        this.errorEvent = index.createEvent(this, "error-event", 7);
        this.balanceTransactions = [];
        this.isLoading = true;
        this.errorMessage = null;
        this.paging = Pagination.pagingDefaults;
        this.pagingParams = {};
        this.columns = defaultColumnsKeys;
        this.handleClickPrevious = (beforeCursor) => {
            this.pagingParams = { before_cursor: beforeCursor };
            this.clickEvent.emit({ name: eventTypes.TableClickActions.previous });
        };
        this.handleClickNext = (afterCursor) => {
            this.pagingParams = { after_cursor: afterCursor };
            this.clickEvent.emit({ name: eventTypes.TableClickActions.next });
        };
        this.rowClickHandler = (e) => {
            const clickedPayoutId = e.target.closest('tr').dataset.rowEntityId;
            if (!clickedPayoutId)
                return;
            const transactionData = this.balanceTransactions.find((payout) => payout.id === clickedPayoutId);
            this.clickEvent.emit({ name: eventTypes.TableClickActions.row, data: transactionData });
        };
    }
    componentWillLoad() {
        this.transactionsTable = new table.Table(this.balanceTransactions, this.columns, payoutTransactionTableColumns, payoutTransactionTableCells);
        checkPkgVersion.checkPkgVersion();
        this.analytics = new Analytics.JustifiAnalytics(this);
        this.initializeApi();
    }
    disconnectedCallback() {
        var _a;
        (_a = this.analytics) === null || _a === void 0 ? void 0 : _a.cleanup();
    }
    ;
    propChanged() {
        this.initializeApi();
    }
    handleError(code, errorMessage, severity) {
        this.isLoading = false;
        this.errorEvent.emit({ errorCode: code, message: errorMessage, severity });
    }
    initializeApi() {
        if (this.payoutId && this.authToken) {
            const getPayoutTransactions = makeGetPayoutTransactions({
                accountId: configState.configState.accountId,
                authToken: this.authToken,
                service: new payout_service.PayoutService()
            });
            getPayoutTransactions({
                params: this.requestParams,
                onSuccess: ({ balanceTransactions, pagingInfo }) => {
                    this.balanceTransactions = balanceTransactions;
                    this.paging = pagingInfo;
                    this.transactionsTable.collectionData = this.balanceTransactions;
                },
                onError: ({ error, code, severity }) => {
                    this.errorMessage = error.message;
                    this.handleError(error, code, severity);
                },
                final: () => {
                    this.isLoading = false;
                }
            });
        }
        else {
            this.errorMessage = 'payout-id and auth-token props are required';
            this.handleError(Plaid.ComponentErrorCodes.MISSING_PROPS, this.errorMessage, Plaid.ComponentErrorSeverity.ERROR);
        }
    }
    get entityId() {
        return this.balanceTransactions.map((transaction) => transaction.id);
    }
    get showEmptyState() {
        return !this.isLoading && !this.errorMessage && this.transactionsTable.rowData.length < 1;
    }
    get showErrorState() {
        return !this.isLoading && !!this.errorMessage;
    }
    get showRowData() {
        return !this.showEmptyState && !this.showErrorState && !this.isLoading;
    }
    get requestParams() {
        return Object.assign({ payout_id: this.payoutId, limit: '15' }, this.pagingParams);
    }
    render() {
        return (index.h(styledHost.StyledHost, { key: 'e7b5b219c34d1c8610fb4113fedd503f0512db2d' }, index.h(table.TableWrapper, { key: '97bbe678c90ba115a091db4fac42560c17f557f2' }, index.h(table.TableComponent, { key: 'ef90c77cd8f19a80e2b3e2b514aa1bc735a1b783' }, index.h(table.TableHead, { key: 'f5f0a00594f099931f1ac1cd4e359bb8fab11ec8' }, index.h(table.TableHeadRow, { key: '559648add19e0426d756f71ecb7bbd5cbcf1349e' }, this.transactionsTable.columnData.map((column) => column))), index.h(table.TableBody, { key: '49f8da0be5dba34ffbb92805d7b5b0fb3eeb6bf7' }, index.h(table.TableLoadingState, { key: '1d61dc2157608f38eb34e64452d7089857e6c67d', columnSpan: this.transactionsTable.columnKeys.length, isLoading: this.isLoading }), index.h(table.TableEmptyState, { key: 'd1f735fdb009740a186a16ea7e15d0320965a286', isEmpty: this.showEmptyState, columnSpan: this.transactionsTable.columnKeys.length }), index.h(table.TableErrorState, { key: 'f50ecc151be5c9143fb69d575a1ef14b3aa17b61', columnSpan: this.transactionsTable.columnKeys.length, errorMessage: this.errorMessage }), this.showRowData && this.transactionsTable.rowData.map((data, index$1) => (index.h(table.TableRow, { "data-test-id": "table-row", "data-row-entity-id": this.entityId[index$1], onClick: this.rowClickHandler }, data)))), this.paging && (index.h(table.TableFoot, { key: '560536e623b2437d613169af2980aec13ebb4531' }, index.h(table.TableFootRow, { key: '42b82920fdd6056bb654db92acf3741aafb13d99' }, index.h(table.TableFootCell, { key: 'e2545104fdac51e0ada942138a08714f213bce63', colSpan: this.transactionsTable.columnData.length }, index.h("pagination-menu", { key: '6a74b19f3fa02f0e86e00783a06b0dfba2bc0ed8', paging: Object.assign(Object.assign({}, this.paging), { handleClickPrevious: this.handleClickPrevious, handleClickNext: this.handleClickNext }) })))))))));
    }
    static get watchers() { return {
        "pagingParams": ["propChanged"],
        "payoutId": ["propChanged"],
        "authToken": ["propChanged"]
    }; }
};

exports.justifi_payout_transactions_list = JustifiPayoutTransactionsList;
