import { __awaiter, __generator } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Menu, ActionIcon, Text } from "@mantine/core";
import { modals } from "@mantine/modals";
import { notifications } from "@mantine/notifications";
import { rem } from "@mantine/styles";
import { IconDotsVertical, IconEdit, IconTrash } from "@tabler/icons-react";
import { Analytics, EventAction } from "../../../utils";
import { useMemo } from "react";
function RowActions(_a) {
    var _this = this;
    var _b;
    var tableOptions = _a.tableOptions, disableCrud = _a.disableCrud, crudAccess = _a.crudAccess, rowActionsPosition = _a.rowActionsPosition, table = _a.table, anayticsCategory = _a.anayticsCategory, crudOptions = _a.crudOptions, tableName = _a.tableName, row = _a.row, rowActionMenuItems = _a.rowActionMenuItems;
    var showMenuByCrudAccess = !disableCrud && ((crudAccess === null || crudAccess === void 0 ? void 0 : crudAccess.editAccess) || (crudAccess === null || crudAccess === void 0 ? void 0 : crudAccess.deleteAccess));
    var showMenu = ((_b = rowActionMenuItems === null || rowActionMenuItems === void 0 ? void 0 : rowActionMenuItems.length) !== null && _b !== void 0 ? _b : 0) > 0 || showMenuByCrudAccess;
    var renderRowActionItems = useMemo(function () {
        if ((rowActionMenuItems === null || rowActionMenuItems === void 0 ? void 0 : rowActionMenuItems.length) === 0) {
            return null;
        }
        return (_jsx(_Fragment, { children: (rowActionMenuItems !== null && rowActionMenuItems !== void 0 ? rowActionMenuItems : []).map(function (action) { return (_jsx(Menu.Item, { icon: action.icon, onClick: function () { var _a; return (_a = action.onClick) === null || _a === void 0 ? void 0 : _a.call(action, row.original); }, children: action.name })); }) }));
    }, [row.original, rowActionMenuItems]);
    return (_jsx(_Fragment, { children: (tableOptions === null || tableOptions === void 0 ? void 0 : tableOptions.renderRowActionMenuItems)
            ? tableOptions.renderRowActionMenuItems({ row: row, table: table })
            : showMenu && (_jsxs(Menu, { withinPortal: true, position: rowActionsPosition === "start" ? "bottom-start" : "bottom-end", shadow: "md", children: [_jsx(Menu.Target, { children: _jsx(ActionIcon, { children: _jsx(IconDotsVertical, { size: "1.5rem" }) }) }), _jsxs(Menu.Dropdown, { children: [renderRowActionItems, (crudAccess === null || crudAccess === void 0 ? void 0 : crudAccess.editAccess) && (_jsx(Menu.Item, { icon: _jsx(IconEdit, { size: rem(18) }), onClick: function () {
                                    var _a;
                                    (_a = crudOptions === null || crudOptions === void 0 ? void 0 : crudOptions.setTableRowObject) === null || _a === void 0 ? void 0 : _a.call(crudOptions, row.original);
                                    table.setEditingRow(row);
                                    Analytics.trackEvent({
                                        category: anayticsCategory,
                                        action: EventAction.Edit,
                                        label: "CRUD | Edit Row Modal Open",
                                    });
                                }, children: "Edit" })), (crudAccess === null || crudAccess === void 0 ? void 0 : crudAccess.deleteAccess) && (_jsx(Menu.Item, { icon: _jsx(IconTrash, { size: rem(18) }), color: "red", onClick: function () { return __awaiter(_this, void 0, void 0, function () {
                                    var _this = this;
                                    return __generator(this, function (_a) {
                                        Analytics.trackEvent({
                                            category: anayticsCategory,
                                            action: EventAction.Delete,
                                            label: "CRUD | Delete Modal Open",
                                        });
                                        modals.openConfirmModal({
                                            title: "Confirm Delete",
                                            centered: true,
                                            children: (_jsxs(Text, { size: "sm", children: ["Are you sure to delete this ", tableName, "?"] })),
                                            labels: { confirm: "Delete", cancel: "Cancel" },
                                            confirmProps: { color: "red" },
                                            onConfirm: function () { return __awaiter(_this, void 0, void 0, function () {
                                                var error_1;
                                                return __generator(this, function (_a) {
                                                    switch (_a.label) {
                                                        case 0:
                                                            if (!(crudOptions === null || crudOptions === void 0 ? void 0 : crudOptions.onDeleteRow)) return [3 /*break*/, 4];
                                                            _a.label = 1;
                                                        case 1:
                                                            _a.trys.push([1, 3, , 4]);
                                                            return [4 /*yield*/, crudOptions.onDeleteRow(row.original)];
                                                        case 2:
                                                            _a.sent();
                                                            Analytics.trackEvent({
                                                                category: anayticsCategory,
                                                                action: EventAction.Delete,
                                                                label: "CRUD | Deleted Row",
                                                            });
                                                            notifications.show({
                                                                color: "teal",
                                                                title: "Success!",
                                                                message: "Record deleted Successfully.",
                                                            });
                                                            return [3 /*break*/, 4];
                                                        case 3:
                                                            error_1 = _a.sent();
                                                            notifications.show({
                                                                color: "red",
                                                                title: "Something went wrong!",
                                                                message: "".concat(tableName, " couldn't be deleted."),
                                                            });
                                                            return [3 /*break*/, 4];
                                                        case 4: return [2 /*return*/];
                                                    }
                                                });
                                            }); },
                                        });
                                        return [2 /*return*/];
                                    });
                                }); }, children: "Delete" }))] })] })) }));
}
export { RowActions };
