"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const AddBox_1 = tslib_1.__importDefault(require("@material-ui/icons/AddBox"));
const Delete_1 = tslib_1.__importDefault(require("@material-ui/icons/Delete"));
const ArrowUpward_1 = tslib_1.__importDefault(require("@material-ui/icons/ArrowUpward"));
const ArrowDownward_1 = tslib_1.__importDefault(require("@material-ui/icons/ArrowDownward"));
const Edit_1 = tslib_1.__importDefault(require("@material-ui/icons/Edit"));
const WxConfirm_1 = tslib_1.__importDefault(require("../WxConfirm"));
function mapActions({ myData, setMyData, onEditRow, insertable, deletable, sortable, readonly, actions, setLoading, }) {
    return [
        ...(insertable && !readonly
            ? [
                {
                    icon: () => react_1.default.createElement(AddBox_1.default, { color: "primary" }),
                    tooltip: '新增',
                    onClick: (event, rowData) => {
                        // 计算出当前行index
                        const rowIndex = myData.findIndex(i => i === rowData);
                        const newRow = {
                            tableData: { isNew: true },
                        };
                        const newData = [
                            ...myData.slice(0, rowIndex + 1),
                            newRow,
                            ...myData.slice(rowIndex + 1),
                        ];
                        // 插入新的一行，并且设置为编辑状态
                        setMyData(newData);
                    },
                },
                rowData => {
                    const disabled = insertable.isEditable ? !insertable.isEditable(rowData) : false;
                    return {
                        disabled,
                        icon: () => react_1.default.createElement(Edit_1.default, { color: disabled ? 'disabled' : 'primary' }),
                        tooltip: '编辑',
                        onClick: onEditRow,
                    };
                },
                ...(insertable.showNewButton
                    ? [
                        {
                            disabled: !insertable.isNewable,
                            icon: () => react_1.default.createElement(AddBox_1.default, { color: insertable.isNewable ? 'primary' : 'disabled' }),
                            tooltip: '新增',
                            isFreeAction: true,
                            onClick: () => {
                                const newRow = {
                                    tableData: { isNew: true },
                                };
                                const newData = [newRow, ...myData];
                                // 插入新的一行，并且设置为编辑状态
                                setMyData(newData);
                            },
                        },
                    ]
                    : []),
            ]
            : []),
        ...(deletable && !readonly
            ? [
                rowData => ({
                    icon: () => react_1.default.createElement(Delete_1.default, { color: deletable(rowData).disabled ? 'disabled' : 'primary' }),
                    disabled: deletable(rowData).disabled,
                    tooltip: '删除',
                    onClick: (event, rowData) => {
                        WxConfirm_1.default(deletable(rowData).confirmOptions);
                    },
                }),
            ]
            : []),
        ...(sortable && !readonly
            ? [
                {
                    icon: () => react_1.default.createElement(ArrowUpward_1.default, { color: "primary" }),
                    tooltip: '上移',
                    onClick: async (event, rowData) => {
                        var _a;
                        const newData = [...myData];
                        const index = rowData.tableData.id;
                        if (index !== 0) {
                            const temp = newData[index];
                            newData[index] = newData[index - 1];
                            newData[index - 1] = temp;
                        }
                        const type = (_a = sortable.onSort) === null || _a === void 0 ? void 0 : _a.call(sortable, rowData, rowData.tableData.id, 'up', newData);
                        if (type instanceof Promise) {
                            setLoading(true);
                            await type;
                            setLoading(false);
                        }
                    },
                },
                {
                    icon: () => react_1.default.createElement(ArrowDownward_1.default, { color: "primary" }),
                    tooltip: '下移',
                    onClick: async (event, rowData) => {
                        var _a;
                        const newData = [...myData];
                        const index = rowData.tableData.id;
                        if (index !== myData.length - 1) {
                            const temp = newData[index];
                            newData[index] = newData[index + 1];
                            newData[index + 1] = temp;
                        }
                        const type = (_a = sortable.onSort) === null || _a === void 0 ? void 0 : _a.call(sortable, rowData, rowData.tableData.id, 'down', newData);
                        if (type instanceof Promise) {
                            setLoading(true);
                            await type;
                            setLoading(false);
                        }
                    },
                },
            ]
            : []),
        ...(readonly ? [] : actions || []),
    ];
}
exports.default = mapActions;
