"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 react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const index_1 = require("@/components/index");
const file_1 = require("@/constants/file");
const system_1 = require("@/pages/system");
const index_2 = require("@/utils/index");
const PAGE_SIZE = 10;
const Table = (0, styled_components_1.default)(antd_1.Table) `
  .ant-table-pagination.ant-pagination {
    margin: 24px 0 0;
  }
`;
const ProjectFileList = (props) => {
    const { type, loading, pageInfo, fileList, deleteFile, fetchFileList, openDrawer, openAuthDrawer } = props;
    // get multi-language
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file } = locale.business;
    // url search params
    const { pathname, search, applicationId, folderId } = (0, index_2.getLocationIfo)((0, react_router_dom_1.useLocation)());
    const handleAuthorize = (open, file) => {
        if (typeof openAuthDrawer === 'function') {
            openAuthDrawer(open, file);
        }
    };
    const authorizeAdmin = (0, react_1.useMemo)(() => type === 'personal' || type === 'application', [type]);
    const columns = [
        {
            title: '',
            dataIndex: '',
            width: 40,
            render: () => react_1.default.createElement(icons_1.FileOutlined, null),
        },
        {
            title: global.nameLabel,
            dataIndex: 'name',
            render: (text, record) => {
                const slugMap = {
                    application: `/applications/${applicationId}/projects/content`,
                    personal: '/workspace/projects/personal',
                    involved: '/workspace/projects/involved',
                    projects: '/projects',
                };
                return (react_1.default.createElement(react_router_dom_1.Link, { onClick: () => {
                        localStorage['foxpage_project_file'] = JSON.stringify(record);
                    }, to: `${slugMap[type]}/content?applicationId=${applicationId}&folderId=${folderId}&fileId=${record.id}&fileType=${record.type}` },
                    react_1.default.createElement(antd_1.Tooltip, { placement: "topLeft", mouseEnterDelay: 1, title: text },
                        react_1.default.createElement(index_1.Name, { style: { maxWidth: type === 'projects' ? 260 : 240 } }, text))));
            },
        },
        {
            title: global.idLabel,
            dataIndex: 'id',
            width: 100,
        },
        {
            title: global.type,
            dataIndex: 'type',
            key: 'type',
            render: (text) => react_1.default.createElement(antd_1.Tag, { color: file_1.suffixTagColor[text] }, file[text]),
        },
        {
            title: global.creator,
            dataIndex: 'creator',
            key: 'creator',
            render: (creator) => (creator === null || creator === void 0 ? void 0 : creator.account) || '--',
        },
        {
            title: global.createTime,
            dataIndex: 'createTime',
            key: 'createTime',
            width: 170,
            render: (text) => (0, index_2.periodFormat)(text, 'unknown'),
        },
        {
            title: global.actions,
            key: '',
            width: authorizeAdmin ? 160 : 80,
            render: (_text, record) => (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.build, disabled: !(record === null || record === void 0 ? void 0 : record.hasContent) },
                    react_1.default.createElement(react_router_dom_1.Link, { to: {
                            pathname: '/builder',
                            search: `?applicationId=${applicationId}&folderId=${folderId}&fileId=${record.id}`,
                            state: { backPathname: pathname, backSearch: search },
                        }, onClick: () => {
                            localStorage['foxpage_project_file'] = JSON.stringify(record);
                        } },
                        react_1.default.createElement(icons_1.BuildOutlined, null))),
                authorizeAdmin && (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.edit, onClick: () => openDrawer(true, record), style: { marginLeft: 8 } },
                        react_1.default.createElement(icons_1.EditOutlined, null)),
                    react_1.default.createElement(antd_1.Popconfirm, { cancelText: global.no, okText: global.yes, title: `${file.deleteMessage}${file[record.type]}?`, onConfirm: () => {
                            deleteFile({
                                id: record.id,
                                applicationId: applicationId,
                                folderId: folderId,
                            });
                        } },
                        react_1.default.createElement(index_1.DeleteButton, { type: "default", size: "small", shape: "circle", title: global.remove, style: { marginLeft: 8 } })),
                    react_1.default.createElement(antd_1.Button, { type: "default", size: "small", shape: "circle", title: global.userPermission, onClick: () => handleAuthorize(true, record) },
                        react_1.default.createElement(icons_1.UserOutlined, null)))))),
        },
    ];
    return (react_1.default.createElement(Table, { rowKey: "id", dataSource: fileList, columns: columns, loading: loading, pagination: pageInfo.total > pageInfo.size
            ? {
                position: ['bottomCenter'],
                current: pageInfo.page,
                pageSize: pageInfo.size,
                total: pageInfo.total,
            }
            : false, onChange: (pagination) => {
            fetchFileList({
                id: folderId,
                applicationId: applicationId,
                page: pagination.current || 1,
                size: pagination.pageSize || PAGE_SIZE,
            });
        } }));
};
exports.default = ProjectFileList;
