"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const OperationDrawer_1 = __importDefault(require("@/components/business/OperationDrawer"));
const index_1 = require("@/components/index");
const system_1 = require("@/pages/system");
const index_2 = require("@/utils/index");
const index_3 = require("./constants/index");
const Container = styled_components_1.default.div `
  display: flex;
  flex-direction: column;
  padding: 24px;
`;
const Button = (0, styled_components_1.default)(antd_1.Button) `
  align-self: flex-end;
  margin-bottom: 24px;
`;
const AuthorizeDrawer = (props) => {
    const { visible, loading, list, users, type, typeId, applicationId, onClose, onFetch, onAdd, onDelete, } = props;
    const [childDrawerVisible, setChildDrawerVisible] = (0, react_1.useState)(false);
    const [selectUserIds, setSelectUserIds] = (0, react_1.useState)([]);
    const [selectRole, setSelectRole] = (0, react_1.useState)(0);
    (0, react_1.useEffect)(() => {
        setSelectUserIds([]);
        setSelectRole(0);
    }, []);
    // get multi-language
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, authorize } = locale.business;
    const handleClose = (0, react_1.useCallback)(() => {
        if (typeof onClose === 'function')
            onClose(false);
    }, [onClose]);
    const handleAuthDelete = (0, react_1.useCallback)((roleId) => {
        if (applicationId && typeId) {
            if (typeof onDelete === 'function' && typeof onFetch === 'function') {
                onDelete({
                    applicationId,
                    ids: [roleId],
                }, () => onFetch({ applicationId, type, typeId }));
            }
        }
    }, [applicationId, typeId, type, onDelete, onFetch]);
    const columns = [
        {
            title: authorize.user,
            dataIndex: 'user',
            key: 'user',
            render: (_, record) => record.target.account || '',
        },
        {
            title: authorize.role,
            dataIndex: 'role',
            key: 'role',
            render: (_, record) => (record.mask ? authorize[index_3.Role[record.mask]] : ''),
        },
        {
            title: global.actions,
            dataIndex: '',
            key: '',
            width: 80,
            render: (_, record) => (react_1.default.createElement(antd_1.Popconfirm, { title: authorize.deleteConfirm, okText: global.yes, cancelText: global.no, onConfirm: () => handleAuthDelete(record.id) },
                react_1.default.createElement(index_1.DeleteButton, { type: "default", size: "small", shape: "circle", title: global.remove }))),
        },
    ];
    const dataSource = list && list.filter((user) => !user.deleted);
    const handleAuthAdd = (0, react_1.useCallback)(() => {
        if (applicationId && typeId && !(0, index_2.objectEmptyCheck)(selectUserIds) && selectRole) {
            if (typeof onAdd === 'function' && typeof onFetch === 'function') {
                onAdd({
                    applicationId,
                    type,
                    typeId,
                    targetIds: selectUserIds,
                    mask: selectRole,
                }, () => {
                    setChildDrawerVisible(false);
                    onFetch({ applicationId, type, typeId });
                });
            }
        }
    }, [applicationId, typeId, type, selectUserIds, selectRole, onAdd, onFetch]);
    const userSelectOptions = users &&
        users.map((user) => ({
            key: user.id,
            label: user.account,
            value: user.id,
        }));
    const userRoleOptions = [
        {
            label: authorize.admin,
            value: index_3.Role['admin'],
        },
        {
            label: authorize.editor,
            value: index_3.Role['editor'],
        },
        {
            label: authorize.publish,
            value: index_3.Role['publish'],
        },
    ];
    return (react_1.default.createElement(OperationDrawer_1.default, { destroyOnClose: true, maskClosable: false, width: 450, title: global.userPermission, open: visible, onClose: handleClose },
        react_1.default.createElement(Container, null,
            react_1.default.createElement(Button, { icon: react_1.default.createElement(icons_1.PlusOutlined, null), onClick: () => setChildDrawerVisible(!childDrawerVisible) }, authorize.add),
            react_1.default.createElement(antd_1.Spin, { spinning: loading },
                react_1.default.createElement(antd_1.Table, { bordered: false, pagination: false, rowKey: (record) => record.id.toString(), columns: columns, dataSource: dataSource })),
            react_1.default.createElement(OperationDrawer_1.default, { destroyOnClose: true, maskClosable: false, width: 450, open: childDrawerVisible, title: authorize.add, actions: react_1.default.createElement(Button, { type: "primary", onClick: handleAuthAdd }, global.apply), onClose: () => setChildDrawerVisible(false) },
                react_1.default.createElement(Container, null,
                    react_1.default.createElement(antd_1.Select, { showArrow: false, mode: "multiple", placeholder: authorize.placeholderUser, options: userSelectOptions, optionFilterProp: "label", onChange: setSelectUserIds, style: { width: '100%' } }),
                    react_1.default.createElement(antd_1.Select, { showArrow: false, placeholder: authorize.placeholderRole, options: userRoleOptions, value: !selectRole ? undefined : selectRole, onChange: setSelectRole, style: { width: '100%', marginTop: 12 } }))))));
};
exports.default = AuthorizeDrawer;
