"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 ProjectContentComponent = (props) => {
    var _a;
    const { type, loading, saveLoading, contentList, extendRecord, fileDetail, drawerOpen, editContent, baseContents, locales, authDrawerOpen = false, authLoading = false, authList = [], userList = [], fetchList, fetchFileDetail, fetchParentFiles, commitToStore, offlineFromStore, updateFileOnlineStatus, openDrawer, fetchLocales, updateContentValue, updateContentTags, saveContent, deleteContent, 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)('');
    // get multi-language
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, content, store } = locale.business;
    // url search params
    const { applicationId, folderId, fileId } = (0, index_2.getLocationIfo)((0, react_router_dom_1.useLocation)());
    // route map with different type
    const typeRouteMap = {
        workspace: '/workspace/projects',
        projects: '/projects',
        application: `/applications/${applicationId}/projects`,
    };
    const projectFolderLink = type === 'application'
        ? `${typeRouteMap[type]}/list?applicationId=${applicationId}`
        : `${typeRouteMap[type]}/list`;
    (0, react_1.useEffect)(() => {
        if (applicationId && fileId) {
            // fetch content list
            fetchFileDetail({ applicationId, ids: [fileId] });
            // fetch folder detail
            fetchParentFiles({
                applicationId,
                id: fileId,
            }, (folder) => {
                const folderName = folder === null || folder === void 0 ? void 0 : folder.name;
                if (folderName)
                    setFolderName(folderName);
            });
        }
    }, []);
    (0, react_1.useEffect)(() => {
        if (applicationId && fileId && (fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.id)) {
            fetchList({
                applicationId,
                fileId,
                fileType: fileDetail.type,
            });
        }
    }, [fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.id]);
    (0, react_1.useEffect)(() => {
        const newTypeId = editContent === null || editContent === void 0 ? void 0 : editContent.id;
        if (newTypeId)
            setTypeId(newTypeId);
    }, [editContent]);
    // fetch content selected authorize list
    (0, react_1.useEffect)(() => {
        if (authDrawerOpen && typeId) {
            if (typeof fetchAuthList === 'function')
                fetchAuthList({
                    applicationId: applicationId,
                    type: 'content',
                    typeId,
                });
        }
    }, [authDrawerOpen, typeId, fetchAuthList]);
    // fetch content selected authorize user available list
    (0, react_1.useEffect)(() => {
        if (authDrawerOpen) {
            if (typeof fetchUserList === 'function')
                fetchUserList({
                    page: PAGE_NUM,
                    size: PAGE_SIZE,
                });
        }
    }, [authDrawerOpen, fetchUserList]);
    const handleCommit = () => {
        if (fileDetail) {
            antd_1.Modal.confirm({
                title: store.commitTitle,
                content: store.commitMsg,
                onOk: () => {
                    commitToStore({
                        id: fileId,
                        applicationId: applicationId,
                        type: fileDetail.type,
                    }, () => {
                        updateFileOnlineStatus(true);
                    });
                },
                okText: store.commitYes,
                cancelText: store.commitNo,
            });
        }
    };
    const handleRevoke = () => {
        antd_1.Modal.confirm({
            title: store.revokeTitle,
            content: store.revokeMsg,
            onOk: () => {
                offlineFromStore({
                    id: fileId,
                    applicationId: applicationId,
                }, () => {
                    updateFileOnlineStatus(false);
                });
            },
            okText: store.commitYes,
            cancelText: store.commitNo,
        });
    };
    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: projectFolderLink },
                        {
                            name: folderName,
                            link: `${typeRouteMap[type]}/detail?applicationId=${applicationId}&folderId=${folderId}`,
                        },
                        {
                            name: (fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.name) || 'File details',
                        },
                    ] }), style: {
                    paddingBottom: 0,
                    maxWidth: type === 'projects' ? global_1.WIDTH_DEFAULT : '100%',
                    overflow: type === 'projects' ? 'unset' : 'hidden auto',
                } },
                react_1.default.createElement("div", { style: { marginBottom: 12, display: 'flex', justifyContent: 'flex-end' } },
                    (fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.online) ? (react_1.default.createElement(antd_1.Button, { type: "default", title: "Revoke To Store", style: { marginRight: 8 }, onClick: handleRevoke },
                        react_1.default.createElement(icons_1.ArrowDownOutlined, null),
                        store.revoke)) : (react_1.default.createElement(antd_1.Button, { type: "default", title: "Commit To Store", style: { marginRight: 8 }, onClick: handleCommit },
                        react_1.default.createElement(icons_1.ArrowUpOutlined, null),
                        store.commit)),
                    react_1.default.createElement(antd_1.Dropdown, { overlay: react_1.default.createElement(antd_1.Menu, null,
                            react_1.default.createElement(antd_1.Menu.Item, { key: "1", onClick: () => {
                                    openDrawer(true, { isBase: true });
                                } }, content.addBase),
                            react_1.default.createElement(antd_1.Menu.Item, { key: "2", onClick: () => {
                                    openDrawer(true);
                                } }, content.addLocale)) },
                        react_1.default.createElement(antd_1.Button, { type: "primary" },
                            react_1.default.createElement(icons_1.PlusOutlined, null),
                            " ",
                            content.add,
                            " ",
                            react_1.default.createElement(icons_1.DownOutlined, null)))),
                react_1.default.createElement(index_3.List, { type: type, loading: loading, contentList: contentList, fileDetail: fileDetail, extendRecord: extendRecord, deleteContent: deleteContent, openDrawer: openDrawer, openAuthDrawer: openAuthDrawer }))),
        react_1.default.createElement(index_3.EditDrawer, { saveLoading: saveLoading, drawerOpen: drawerOpen, fileDetail: fileDetail, editContent: editContent, locales: locales, baseContents: baseContents, closeDrawer: openDrawer, fetchLocales: fetchLocales, updateContentValue: updateContentValue, updateContentTags: updateContentTags, saveContent: saveContent }),
        type !== 'projects' && (react_1.default.createElement(index_1.AuthorizeDrawer, { type: "folder", typeId: typeId, applicationId: applicationId, visible: authDrawerOpen, loading: authLoading, list: authList, users: userList, onClose: openAuthDrawer, onFetch: fetchAuthList, onAdd: saveUser, onDelete: deleteUser }))));
};
exports.default = ProjectContentComponent;
