import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { CheckOutlined, CloseOutlined } from '@ant-design/icons';
import { Table, Tabs } from 'antd';
import { QueryRenderer } from '@cubejs-client/react';
import { format } from 'sql-formatter';
import PrismCode from '../PrismCode';
import { FatalError } from './Error/FatalError';
const CachePane = ({ query }) => (_jsx(QueryRenderer, { loadSql: true, query: { ...query, renewQuery: true }, render: ({ sqlQuery, resultSet, error }) => {
        if (error) {
            return _jsx(FatalError, { error: error });
        }
        const rs = resultSet?.serialize() || {};
        const rawQuery = sqlQuery?.rawQuery();
        const loadResponse = rs.loadResponse?.results[0];
        return (_jsxs(Tabs, { defaultActiveKey: "refreshKeys", tabBarExtraContent: _jsxs("span", { children: ["Last Refresh Time:\u00A0", _jsx("b", { children: loadResponse?.lastRefreshTime })] }), children: [_jsx(Tabs.TabPane, { tab: "Refresh Keys", children: _jsx(Table, { loading: !sqlQuery, pagination: false, scroll: { x: true }, columns: [
                            {
                                title: 'Refresh Key SQL',
                                key: 'refreshKey',
                                render: (text, record) => (_jsx(PrismCode, { code: format(record[0]) })),
                            },
                            {
                                title: 'Value',
                                key: 'value',
                                render: (text, record) => (_jsx(PrismCode, { code: loadResponse?.refreshKeyValues &&
                                        rawQuery &&
                                        JSON.stringify(loadResponse.refreshKeyValues[rawQuery.cacheKeyQueries.indexOf(record)], null, 2) })),
                            },
                        ], dataSource: rawQuery?.cacheKeyQueries }) }, "refreshKeys"), _jsx(Tabs.TabPane, { tab: "Pre-aggregations", children: _jsx(Table, { loading: !sqlQuery, pagination: false, scroll: { x: true }, columns: [
                            {
                                title: 'Table Name',
                                key: 'tableName',
                                dataIndex: 'tableName',
                                render: (text) => _jsx("b", { children: text }),
                            },
                            {
                                title: 'Refresh Key SQL',
                                key: 'refreshKey',
                                dataIndex: 'invalidateKeyQueries',
                                render: (refreshKeyQueries) => refreshKeyQueries.map((q) => (_jsx(PrismCode, { code: format(q[0]) }, q[0]))),
                            },
                            {
                                title: 'Refresh Key Value',
                                key: 'value',
                                render: (text, record) => {
                                    let refreshKeyValues = loadResponse?.usedPreAggregations?.[record.tableName]?.refreshKeyValues;
                                    if (Array.isArray(refreshKeyValues)) {
                                        refreshKeyValues = refreshKeyValues.reduce((a, b) => a.concat(b), []);
                                    }
                                    return refreshKeyValues?.map((k) => (_jsx(PrismCode, { code: JSON.stringify(k, null, 2) }, JSON.stringify(k))));
                                }
                            },
                        ], dataSource: rawQuery?.preAggregations }) }, "preAggregations"), _jsx(Tabs.TabPane, { tab: "Rollup Match Results", children: _jsx(Table, { loading: !sqlQuery, pagination: false, scroll: { x: true }, columns: [
                            {
                                title: 'Rollup Name',
                                key: 'name',
                                dataIndex: 'name',
                                render: (text) => _jsx("b", { children: text }),
                            },
                            {
                                title: 'Rollup Definition',
                                key: 'rollup',
                                dataIndex: 'references',
                                render: (text) => (_jsx(PrismCode, { code: JSON.stringify(text, null, 2) })),
                            },
                            {
                                title: 'Can Be Used',
                                key: 'canUsePreAggregation',
                                dataIndex: 'canUsePreAggregation',
                                render: (text) => text ? (_jsx(CheckOutlined, { style: {
                                        color: '#52c41a',
                                        fontSize: '2em',
                                    } })) : (_jsx(CloseOutlined, { style: {
                                        color: '#c2371b',
                                        fontSize: '2em',
                                    } })),
                            },
                        ], dataSource: rawQuery?.rollupMatchResults }) }, "rollupMatchResults")] }));
    } }));
export default CachePane;
//# sourceMappingURL=CachePane.js.map