"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;
};
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 index_1 = require("@/components/index");
const global_1 = require("@/constants/global");
const system_1 = require("@/pages/system");
const index_2 = require("@/utils/index");
const index_3 = require("./components/index");
const PAGE_NUM = 1;
const PAGE_SIZE = 999;
const ProjectFileComponent = (props) => {
    var _a;
    const { type, pageInfo, loading, saveLoading, fileList, drawerOpen, editFile, authDrawerOpen = false, authLoading = false, authList = [], userList = [], fetchFileList, fetchParentFiles, openDrawer, updateEditFile, saveFile, deleteFile, fetchAuthList, fetchUserList, saveUser, deleteUser, openAuthDrawer, } = props;
    const [folderName, setFolderName] = (0, react_1.useState)(((_a = (0, index_2.getProjectFolder)()) === null || _a === void 0 ? void 0 : _a.name) || 'Project details');
    const [typeId, setTypeId] = (0, react_1.useState)('');
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file } = locale.business;
    // url search params
    const { applicationId, folderId } = (0, index_2.getLocationIfo)((0, react_router_dom_1.useLocation)());
    // route map with different type
    const typeRouteMap = {
        application: `/applications/${applicationId}/projects/list?applicationId=${applicationId}`,
        involved: '/workspace/projects/involved/list',
        personal: '/workspace/projects/personal/list',
        projects: '/projects/list',
    };
    (0, react_1.useEffect)(() => {
        if (applicationId && folderId) {
            // fetch file list
            fetchFileList({
                applicationId,
                id: folderId,
                page: pageInfo.page,
                size: pageInfo.size,
            });
            // fetch folder detail
            fetchParentFiles({
                applicationId,
                id: folderId,
            }, (folder) => {
                const folderName = folder === null || folder === void 0 ? void 0 : folder.name;
                if (folderName)
                    setFolderName(folderName);
            });
        }
    }, []);
    (0, react_1.useEffect)(() => {
        const newTypeId = editFile === null || editFile === void 0 ? void 0 : editFile.id;
        if (newTypeId)
            setTypeId(newTypeId);
    }, [editFile]);
    // fetch file selected authorize list
    (0, react_1.useEffect)(() => {
        if (authDrawerOpen && applicationId && typeId) {
            if (typeof fetchAuthList === 'function')
                fetchAuthList({
                    applicationId: applicationId,
                    type: 'file',
                    typeId,
                });
        }
    }, [authDrawerOpen, typeId, fetchAuthList]);
    // fetch file selected authorize user available list
    (0, react_1.useEffect)(() => {
        if (authDrawerOpen) {
            if (typeof fetchUserList === 'function')
                fetchUserList({
                    page: PAGE_NUM,
                    size: PAGE_SIZE,
                });
        }
    }, [authDrawerOpen, fetchUserList]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.Content, null,
            react_1.default.createElement(index_1.FoxPageContent, { breadcrumb: react_1.default.createElement(index_1.FoxPageBreadcrumb, { breadCrumb: [
                        {
                            name: global.project,
                            link: typeRouteMap[type],
                        },
                        { name: folderName },
                    ] }), style: {
                    maxWidth: type === 'projects' ? global_1.WIDTH_DEFAULT : '100%',
                    overflow: type === 'projects' ? 'unset' : 'hidden auto',
                } },
                react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement("div", { style: { marginBottom: 12, display: 'flex', justifyContent: 'flex-end' } },
                        react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => openDrawer(true) },
                            react_1.default.createElement(icons_1.PlusOutlined, null),
                            " ",
                            file.add)),
                    react_1.default.createElement(index_3.List, { type: type, loading: loading, pageInfo: pageInfo, fileList: fileList, fetchFileList: fetchFileList, deleteFile: deleteFile, openDrawer: openDrawer, openAuthDrawer: openAuthDrawer })))),
        react_1.default.createElement(index_3.EditDrawer, { drawerOpen: drawerOpen, saveLoading: saveLoading, pageInfo: pageInfo, editFile: editFile, updateEditFile: updateEditFile, saveFile: saveFile, fetchFileList: fetchFileList, closeDrawer: openDrawer }),
        type !== 'projects' && (react_1.default.createElement(index_1.AuthorizeDrawer, { type: "file", typeId: typeId, applicationId: applicationId, visible: authDrawerOpen, loading: authLoading, list: authList, users: userList, onClose: openAuthDrawer, onFetch: fetchAuthList, onAdd: saveUser, onDelete: deleteUser }))));
};
exports.default = ProjectFileComponent;
