import { __assign, __spreadArray } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Flex, Button } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { IconPlus, IconDownload } from "@tabler/icons-react";
import { ExportToCsv } from "export-to-csv-fix-source-map";
import { Analytics, EventAction } from "../../../utils";
function TopToolbar(_a) {
    var tableName = _a.tableName, table = _a.table, crudAccess = _a.crudAccess, anayticsCategory = _a.anayticsCategory, tableOptions = _a.tableOptions, columns = _a.columns;
    var csvOptions = {
        fieldSeparator: ",",
        filename: "".concat(tableName, " Data"),
        quoteStrings: '"',
        decimalSeparator: ".",
        showLabels: true,
        useBom: true,
        useKeysAsHeaders: false,
    };
    return (_jsx(Flex, { justify: "end", pr: 5, sx: {
            flex: 1,
        }, children: _jsxs(Flex, { direction: "row-reverse", gap: "md", children: [(crudAccess === null || crudAccess === void 0 ? void 0 : crudAccess.createAccess) && (_jsx(Button, { onClick: function () {
                        table.setCreatingRow(true);
                        Analytics.trackEvent({
                            category: anayticsCategory,
                            action: EventAction.Create,
                            label: "CRUD | Create Modal Open",
                        });
                    }, leftIcon: _jsx(IconPlus, {}), children: tableName })), (tableOptions === null || tableOptions === void 0 ? void 0 : tableOptions.renderCustomTopToolbarComponent) &&
                    (tableOptions === null || tableOptions === void 0 ? void 0 : tableOptions.renderCustomTopToolbarComponent({ table: table })), (table.getIsSomeRowsSelected() || table.getIsAllRowsSelected()) && (_jsx(Button, { onClick: function () {
                        // get selected rows
                        var rows = table.getSelectedRowModel().rows;
                        // Get visible columns' keys
                        var visibleColumns = columns
                            .filter(function (c) {
                            return table.getState().columnVisibility[c.accessorKey] !== false;
                        })
                            .map(function (c) { return c.accessorKey; });
                        // Prepare header columns for CSV
                        var headerColumns = visibleColumns.map(function (key) { return columns.find(function (c) { return c.accessorKey === key; }).header; });
                        csvOptions.headers = __spreadArray(["Sr. No."], headerColumns, true);
                        var exportRows = rows
                            .map(function (row) {
                            return Object.fromEntries(visibleColumns.map(function (key) { return [key, row.original[key !== null && key !== void 0 ? key : ""]]; }));
                        }) // add only those columns which are defined and in same order
                            .map(function (row, index) { return (__assign({ srNo: index + 1 }, row)); }); // adds Sr. No field at starting of row
                        var csvExporter = new ExportToCsv(csvOptions);
                        csvExporter.generateCsv(exportRows);
                        Analytics.trackEvent({
                            category: anayticsCategory,
                            action: EventAction.Export,
                            label: "Rows Selected: ".concat(rows.length),
                        });
                        notifications.show({
                            color: "teal",
                            title: "Success!",
                            message: "Data exported Successfully.",
                        });
                    }, leftIcon: _jsx(IconDownload, {}), variant: "outline", children: "Export Selected Rows" }))] }) }));
}
export { TopToolbar };
