var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from 'react';
import { Type } from '@sinclair/typebox';
import { implementRuntimeComponent } from '@subscan/widget-runtime';
import { COMPONENTS_CATEGORY, PRESET_PROPERTY_CATEGORY, VERSION } from 'config/constants';
import { StyledFont14, StyledModuleBox, StyledScrollX } from 'ui/common';
import { StyledSortAsc, StyledSortBox, StyledSortDesc, StyledTable, StyledTd, StyledTh, StyledTr } from './styled';
import { useStateValue } from 'hooks/useStateValue';
import { css } from '@emotion/css';
import { BaseColumnSpecObject, RenderColumnValue } from '../common/RenderColumnValue';
var CssBox = css(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n  padding: 10px 20px;\n"], ["\n  padding: 10px 20px;\n"])));
export var ColumnSpec = Type.Object(__assign({ title: Type.String({
        title: 'Title',
        category: PRESET_PROPERTY_CATEGORY.Basic,
    }), width: Type.String({
        title: 'Width',
        category: PRESET_PROPERTY_CATEGORY.Basic,
    }), dataKey: Type.String({
        title: 'Key',
        category: PRESET_PROPERTY_CATEGORY.Basic,
        description: 'The key corresponding to the column data in the data item is used to display the value',
    }), sort: Type.KeyOf(Type.Object({
        disabled: Type.String(),
        default: Type.String(),
        desc: Type.String(),
        asc: Type.String(),
    }), {
        title: 'Sort',
        category: PRESET_PROPERTY_CATEGORY.Basic,
    }) }, BaseColumnSpecObject));
var PropsSpec = Type.Object({
    data: Type.Array(Type.Any(), {
        title: 'Data',
        category: PRESET_PROPERTY_CATEGORY.Data,
        weight: 0,
        widget: 'core/v1/expression',
    }),
    columns: Type.Array(ColumnSpec, {
        title: 'Columns',
        description: '',
        category: PRESET_PROPERTY_CATEGORY.Columns,
        widget: 'core/v1/array',
        widgetOptions: {
            displayedKeys: ['title'],
        },
        weight: 0,
    }),
});
var StateSpec = Type.Object({
    currentSortKey: Type.String(),
    currentSortValue: Type.String(),
});
var RenderSort = function (_a) {
    var sort = _a.sort, dataKey = _a.dataKey, cb = _a.cb;
    var _b = useState(sort || 'default'), currentSort = _b[0], setCurrentSortSort = _b[1];
    useEffect(function () {
        setCurrentSortSort(sort);
    }, [sort]);
    var handleClick = useCallback(function () {
        var _sort = 'default';
        if (currentSort === 'asc') {
            _sort = 'desc';
        }
        if (currentSort === 'default' || !currentSort) {
            _sort = 'asc';
        }
        if (currentSort === 'desc') {
            _sort = 'default';
        }
        var stats = { key: dataKey, sort: _sort };
        cb && cb(stats);
    }, [cb, currentSort, dataKey]);
    if (currentSort === 'disabled') {
        return null;
    }
    return (_jsxs(StyledSortBox, __assign({ onClick: handleClick }, { children: [_jsx(StyledSortAsc, { className: currentSort === 'asc' ? 'active' : '' }), _jsx(StyledSortDesc, { className: currentSort === 'desc' ? 'active' : '' })] })));
};
export default implementRuntimeComponent({
    version: VERSION.Core,
    metadata: {
        name: 'table',
        displayName: 'Table',
        description: 'Table Components',
        exampleProperties: {
            data: [
                {
                    from: '2rGH1BB1E6fvTqiVrHMwNw8r5VrFYznvafn2Uf7amvYdCZ9f',
                    to: '2qSbd2umtD4KmV2X4zZk5QkCvmYKyiR2ysAeM1Eca6vcvg7N',
                    extrinsic_index: '10426613-1',
                    success: false,
                    hash: '0x4b786c3d4740fcb90bfa593c1335a65fc2ff425114b3060ae377b6981c50e3d8',
                    block_num: 10426613,
                    block_timestamp: 1664176458,
                    module: 'balances',
                    amount: '86.13245',
                    amount_v2: '86132450000',
                    fee: '114600000',
                    nonce: 7,
                    asset_symbol: '',
                    asset_type: '',
                    from_account_display: { address: '2rGH1BB1E6fvTqiVrHMwNw8r5VrFYznvafn2Uf7amvYdCZ9f' },
                    to_account_display: {
                        address: '2qSbd2umtD4KmV2X4zZk5QkCvmYKyiR2ysAeM1Eca6vcvg7N',
                        display: '0x245b4775082c144c22a4874b0fba8c70c510c5ae',
                    },
                    event_idx: 1,
                },
                {
                    from: '2rGH1BB1E6fvTqiVrHMwNw8r5VrFYznvafn2Uf7amvYdCZ9f',
                    to: '2qSbd2umtD4KmV2X4zZk5QkCvmYKyiR2ysAeM1Eca6vcvg7N',
                    extrinsic_index: '10426613-1',
                    success: false,
                    hash: '0x4b786c3d4740fcb90bfa593c1335a65fc2ff425114b3060ae377b6981c50e3d8',
                    block_num: 10426613,
                    block_timestamp: 1664176458,
                    module: 'balances',
                    amount: '86.13245',
                    amount_v2: '86132450000',
                    fee: '114600000',
                    nonce: 7,
                    asset_symbol: '',
                    asset_type: '',
                    from_account_display: { address: '2rGH1BB1E6fvTqiVrHMwNw8r5VrFYznvafn2Uf7amvYdCZ9f' },
                    to_account_display: {
                        address: '2qSbd2umtD4KmV2X4zZk5QkCvmYKyiR2ysAeM1Eca6vcvg7N',
                        display: '0x245b4775082c144c22a4874b0fba8c70c510c5ae',
                    },
                    event_idx: 1,
                },
            ],
            columns: [
                {
                    title: 'Extrinsic ID',
                    type: 'link',
                    width: '',
                    dataKey: 'extrinsic_index',
                    prePath: '/extrinsic',
                    ellipsis: false,
                    sort: 'disabled',
                },
                {
                    title: 'Block',
                    type: 'link',
                    width: '',
                    dataKey: 'block_num',
                    prePath: '/block',
                    ellipsis: false,
                    sort: 'disabled',
                },
                {
                    title: 'Time',
                    type: 'time',
                    width: '',
                    dataKey: 'block_timestamp',
                    sort: 'disabled',
                },
                {
                    title: 'From',
                    type: 'link',
                    width: '',
                    dataKey: 'from',
                    prePath: '/account',
                    sort: 'disabled',
                },
            ],
        },
        annotations: {
            category: COMPONENTS_CATEGORY.Display,
        },
    },
    spec: {
        properties: PropsSpec,
        state: StateSpec,
        methods: {},
        slots: {},
        styleSlots: ['content'],
        events: [],
    },
})(function (_a) {
    var data = _a.data, columns = _a.columns, elementRef = _a.elementRef, mergeState = _a.mergeState, services = _a.services;
    var _b = useStateValue('', mergeState, true, 'currentSortKey'), currentSortKey = _b[0], setCurrentSortKey = _b[1];
    var _c = useStateValue('', mergeState, true, 'currentSortValue'), currentSortValue = _c[0], setCurrentSortValue = _c[1];
    return (_jsx(StyledModuleBox, __assign({ className: CssBox, ref: elementRef }, { children: _jsx(StyledScrollX, { children: _jsxs(StyledTable, { children: [_jsx("thead", { children: _jsx("tr", { children: columns === null || columns === void 0 ? void 0 : columns.map(function (thData, tdIndex) {
                                return (_jsxs(StyledTh, { children: [_jsx(StyledFont14, __assign({ bold: true, nowrap: true }, { children: thData.title })), _jsx(RenderSort, { cb: function (_a) {
                                                var key = _a.key, sort = _a.sort;
                                                setCurrentSortKey(key);
                                                setCurrentSortValue(sort);
                                                mergeState({ currentSortKey: key, currentSortValue: sort });
                                            }, dataKey: thData.dataKey, sort: currentSortKey === thData.dataKey
                                                ? currentSortValue
                                                : thData.sort })] }, "".concat(thData.title).concat(tdIndex)));
                            }) }) }), _jsx("tbody", { children: data === null || data === void 0 ? void 0 : data.map(function (trData, trIndex) {
                            return (_jsx(StyledTr, { children: columns.map(function (column, tdIndex) {
                                    return (_jsx(StyledTd, { children: _jsx(RenderColumnValue, __assign({ stateManager: services.stateManager, value: trData[column.dataKey] }, column)) }, "".concat(trData.title).concat(tdIndex)));
                                }) }, "".concat(trIndex)));
                        }) })] }) }) })));
});
var templateObject_1;
