/*!
 * Crafted with ❤ by Salla
 */
'use strict';

var index = require('./index-vstkdqRz.js');

const sallaWalletCss = ".s-wallet-table{min-width:100%}.s-wallet-table-balance-container{display:flex !important;align-items:center !important;justify-content:flex-start !important;gap:1rem}.s-wallet-table-balance-icon{border-radius:50%;width:52px;height:52px;font-size:25px;padding:16px 13px;background:var(--color-primary-reverse);color:var(--color-primary)}.s-wallet-table-empty-state{display:flex;flex-direction:column;align-items:center}.s-wallet-table-empty-state i{font-size:80px;margin:4rem 0 2rem;color:var(--infinte-color)}.s-wallet-table-transaction-status{padding:6px 19px !important;border-radius:20px;width:max-content;display:flex;align-items:center}.s-wallet-table-transaction-status-default{background:#fff6eb;color:#a46f29}.s-wallet-table-transaction-status-cashback{background:#effbf6;color:#00af6c}.s-wallet-table-transaction-status-refund{background:#ecf3fe;color:#5196f3}.s-wallet-table-transaction-status-purchase{background:#feecec;color:#f55157}.s-wallet-table-transaction-status-icons-success{color:#00af6c}";

const SallaWalletTable = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.isLoading = false;
        this.nextPageUrl = '';
        this.handleClick = () => {
            this.loadMoreTransactions();
        };
    }
    componentWillLoad() {
        return salla.onReady().then(() => this.loadTransactions());
    }
    async loadTransactions() {
        this.isLoading = true;
        try {
            let url = `/balance/wallet?per_page=10&page=1`;
            let resp = await salla.api.request(url);
            this.nextPageUrl = resp.data.transactions.next_page_url;
            this.transactionsArray = resp.data.transactions;
            this.balance = resp.data.balance;
        }
        catch (error) {
            console.error('Error loading wallet transactions', error);
        }
        finally {
            this.isLoading = false;
        }
    }
    async loadMoreTransactions() {
        this.isLoading = true;
        try {
            let url = this.nextPageUrl || '/balance/wallet?page=2&per_page=10';
            let resp = await salla.api.request(url);
            this.nextPageUrl = resp.data.transactions.next_page_url;
            this.transactionsArray.push(...resp.data.transactions.data);
            this.balance = resp.data.balance;
        }
        catch (error) {
            console.error('Error loading wallet transactions', error);
        }
        finally {
            this.isLoading = false;
        }
    }
    render() {
        if (this.transactionsArray.length > 0) {
            return (index.h(index.Host, null, index.h("table", { class: "s-wallet-table" }, index.h("tbody", { class: "s-wallet-table-tbody" }, index.h("tr", { class: "s-table__tr" }, index.h("td", null, index.h("div", { class: "s-wallet-table-balance-container" }, index.h("i", { class: "s-wallet-table-balance-icon sicon-wallet" }), index.h("div", null, index.h("div", { innerHTML: salla.money(this.balance) }), index.h("div", { class: "font-normal text-gray-400" }, ' ', salla.lang.get('pages.wallet.balance')))))))), index.h("h2", { style: { 'margin-top': '32px' } }, salla.lang.get('pages.wallet.title')), index.h("table", { class: "s-wallet-table" }, index.h("thead", { class: "s-wallet-table-head" }, index.h("tr", { class: "s-wallet-table-head-tr" }, index.h("th", { scope: "col" }, salla.lang.get('pages.wallet.transaction_number')), index.h("th", { scope: "col", class: "s-wallet-table-head-tr-th" }, salla.lang.get('pages.wallet.amount')), index.h("th", { scope: "col", class: "s-wallet-table-head-tr-th" }, salla.lang.get('pages.wallet.date')), index.h("th", { scope: "col", class: "s-wallet-table-head-tr-th" }, salla.lang.get('pages.wallet.order_no')), index.h("th", { scope: "col", class: "s-wallet-table-head-tr-th" }, salla.lang.get('pages.wallet.expiry_date')), index.h("th", { scope: "col", class: "s-wallet-table-head-tr-th" }, salla.lang.get('pages.wallet.transaction_type'), ' '))), index.h("tbody", { class: "s-wallet-table-tbody" }, this.transactionsArray.map(transaction => (index.h("tr", { class: "s-wallet-table-tbody-tr s-wallet-table-tbody-tr-shadow animated" }, index.h("td", { class: "s-wallet-table-tbody-tr-td" }, index.h("div", { class: "s-wallet-table-tbody-tr-td-content" }, index.h("span", { class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.transaction_number'), ":"), transaction.id && (index.h("span", { class: "hidden md:inline-block" }, "#", transaction.id)), index.h("div", { class: "flex items-center md:hidden" }, index.h("salla-button", { class: "relative", color: "dark", shape: "link", "data-content": transaction.order_id }, transaction.order_id && index.h("span", null, "#", transaction.order_id))))), index.h("td", { id: "main123", class: "s-wallet-table-tbody-tr-td" }, index.h("div", { id: "main124", class: "s-wallet-table-tbody-tr-td-content" }, index.h("span", { id: "main125", class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.amount'), ":"), index.h("div", { innerHTML: salla.money(transaction.amount) }))), index.h("td", { class: "s-wallet-table-tbody-tr-td" }, index.h("div", { class: "s-wallet-table-tbody-tr-td-content" }, index.h("span", { class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.date'), ":"), transaction.created_at &&
                new Date(transaction.created_at * 1000).toLocaleDateString(salla.lang.locale, {
                    weekday: 'long',
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric',
                    hour: 'numeric',
                }))), index.h("td", { id: "main133", class: "s-wallet-table-tbody-tr-td" }, index.h("div", { id: "main134", class: "s-wallet-table-tbody-tr-td-content" }, index.h("span", { id: "main135", class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.order_no'), ":"), index.h("span", { class: "rtl:mr-auto ltr:ml-auto md:mx-0" }, transaction.order_id && index.h("span", null, "#", transaction.order_id)))), index.h("td", { id: "main143", class: "s-wallet-table-tbody-tr-td" }, index.h("div", { id: "main144", class: "s-wallet-table-tbody-tr-td-content" }, index.h("span", { id: "main145", class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.expiry_date'), ":"), index.h("span", null, transaction.expired_at &&
                new Date(transaction.expired_at * 1000).toLocaleDateString(salla.lang.locale, {
                    weekday: 'long',
                    year: 'numeric',
                    month: 'long',
                    day: 'numeric',
                    hour: 'numeric',
                })))), index.h("td", { class: "s-wallet-table-tbody-tr-td" }, index.h("div", { class: "s-wallet-table-tbody-tr-td-content" }, index.h("div", null, index.h("span", { class: "s-wallet-mobile-title" }, salla.lang.get('pages.wallet.transaction_type'), ":")), index.h("div", { class: {
                    's-wallet-table-transaction-status-default': true,
                    's-wallet-table-transaction-status-cashback': transaction.action_type === 'cashback',
                    's-wallet-table-transaction-status-refund': transaction.action_type === 'refund',
                    's-wallet-table-transaction-status-purchase': transaction.action_type === 'purchase_by_wallet',
                } }, !!transaction.description && (index.h("i", { id: transaction.id.toString(), class: "sicon-information text-lg rtl:ml-4 ltr:mr-4 s-wallet-table-transaction-status-icon-success" })), !!transaction.description && (index.h("salla-tooltip", { text: transaction.description, targetId: transaction.id.toString() })), index.h("span", null, transaction.title))))))))), index.h("div", { class: "s-infinite-scroll-wrapper" }, !!this.nextPageUrl && (index.h("salla-button", { onClick: this.handleClick, loading: this.isLoading }, salla.lang.get('common.elements.load_more'))))));
        }
        else {
            return (index.h(index.Host, null, index.h("h2", { style: { 'margin-top': '32px' } }, salla.lang.get('pages.wallet.title')), index.h("div", { class: "s-wallet-table-empty-state" }, index.h("i", { class: "sicon-full-wallet" }), index.h("div", { class: "text-center p-3 text-xl font-bold " }, salla.lang.get('pages.wallet.no_transactions')), index.h("div", { class: "text-center text-gray-400 text-sm font-normal " }, salla.lang.get('pages.wallet.zero_balance'), ' '))));
        }
    }
};
SallaWalletTable.style = sallaWalletCss;

exports.salla_wallet = SallaWalletTable;
