import { __assign, __awaiter, __generator } from "tslib";
import { notifications } from "@mantine/notifications";
import { Analytics, EventAction, getDifferingValues, updateValues, } from "../../../utils";
function onCreatingRowSave(_a) {
    var e = _a.e, tableName = _a.tableName, crudOptions = _a.crudOptions, anayticsCategory = _a.anayticsCategory;
    return __awaiter(this, void 0, void 0, function () {
        var tableRowObject, onCreateRow, setTableRowObject, values, hasValues, error_1;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    tableRowObject = crudOptions.tableRowObject, onCreateRow = crudOptions.onCreateRow, setTableRowObject = crudOptions.setTableRowObject;
                    values = __assign(__assign({}, e.values), tableRowObject);
                    hasValues = Object.values(values).some(function (value) { return !!value; });
                    if (!(onCreateRow && hasValues)) return [3 /*break*/, 4];
                    _b.label = 1;
                case 1:
                    _b.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, onCreateRow(values)];
                case 2:
                    _b.sent();
                    Analytics.trackEvent({
                        category: anayticsCategory,
                        action: EventAction.Create,
                        label: "CRUD | Created New Record",
                    });
                    setTableRowObject === null || setTableRowObject === void 0 ? void 0 : setTableRowObject({});
                    notifications.show({
                        color: "teal",
                        title: "Success!",
                        message: "".concat(tableName, " added Successfully."),
                    });
                    return [3 /*break*/, 4];
                case 3:
                    error_1 = _b.sent();
                    notifications.show({
                        color: "red",
                        title: "Something went wrong!",
                        message: "".concat(tableName, " couldn't be added."),
                    });
                    return [3 /*break*/, 4];
                case 4:
                    e.exitCreatingMode();
                    return [2 /*return*/];
            }
        });
    });
}
function onEditingRowSave(_a) {
    var e = _a.e, tableName = _a.tableName, crudOptions = _a.crudOptions, anayticsCategory = _a.anayticsCategory;
    return __awaiter(this, void 0, void 0, function () {
        var _b, tableRowObject, onEditSaveRow, setTableRowObject, differingValues, changedValues, values, error_2;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    _b = crudOptions !== null && crudOptions !== void 0 ? crudOptions : {}, tableRowObject = _b.tableRowObject, onEditSaveRow = _b.onEditSaveRow, setTableRowObject = _b.setTableRowObject;
                    differingValues = getDifferingValues(e.row.original, tableRowObject !== null && tableRowObject !== void 0 ? tableRowObject : {});
                    changedValues = updateValues(e.row.original, e.values);
                    values = updateValues(changedValues, differingValues);
                    if (!onEditSaveRow) return [3 /*break*/, 4];
                    _c.label = 1;
                case 1:
                    _c.trys.push([1, 3, , 4]);
                    return [4 /*yield*/, onEditSaveRow(values)];
                case 2:
                    _c.sent();
                    Analytics.trackEvent({
                        category: anayticsCategory,
                        action: EventAction.Edit,
                        label: "CRUD | Updated the Record",
                    });
                    setTableRowObject === null || setTableRowObject === void 0 ? void 0 : setTableRowObject({});
                    notifications.show({
                        color: "teal",
                        title: "Success!",
                        message: "".concat(tableName, " updated Successfully."),
                    });
                    return [3 /*break*/, 4];
                case 3:
                    error_2 = _c.sent();
                    notifications.show({
                        color: "red",
                        title: "Something went wrong!",
                        message: "".concat(tableName, " couldn't be updated."),
                    });
                    return [3 /*break*/, 4];
                case 4:
                    e.exitEditingMode();
                    return [2 /*return*/];
            }
        });
    });
}
export { onCreatingRowSave, onEditingRowSave };
