"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 global_1 = require("@/constants/global");
const system_1 = require("@/pages/system");
const location_info_1 = require("@/utils/location-info");
const index_2 = require("./components/index");
const PAGE_NUM = 1;
const PAGE_SIZE = 999;
const Header = styled_components_1.default.div `
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
`;
const ProjectFolderComponent = (props) => {
    const { type, organizationId, loading, saveLoading, pageInfo, applicationList, projectList, editProject, drawerOpen, authDrawerOpen = false, authLoading = false, authList = [], userList = [], openDrawer, fetchList, fetchApps, updateEditProject, saveProject, deleteProject, fetchAuthList, fetchUserList, saveUser, deleteUser, openAuthDrawer, } = props;
    const [applicationId, setApplicationId] = (0, react_1.useState)('');
    const [search, setSearch] = (0, react_1.useState)('');
    const [typeId, setTypeId] = (0, react_1.useState)('');
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, project } = locale.business;
    // location search params
    const { applicationId: queryApplicationId } = (0, location_info_1.getLocationIfo)((0, react_router_dom_1.useLocation)());
    const isFromProject = !type && !applicationId;
    (0, react_1.useEffect)(() => {
        if (queryApplicationId)
            setApplicationId(queryApplicationId);
    }, [queryApplicationId]);
    (0, react_1.useEffect)(() => {
        if (organizationId)
            fetchList({
                organizationId,
                applicationId,
                page: pageInfo.page,
                size: pageInfo.size,
            });
    }, [organizationId, applicationId, fetchList]);
    // get select project detail info
    (0, react_1.useEffect)(() => {
        var _a;
        const newApplicationId = (_a = editProject === null || editProject === void 0 ? void 0 : editProject.application) === null || _a === void 0 ? void 0 : _a.id;
        if (newApplicationId)
            setApplicationId(newApplicationId);
        const newTypeId = editProject === null || editProject === void 0 ? void 0 : editProject.id;
        if (newTypeId)
            setTypeId(newTypeId);
    }, [editProject]);
    // fetch project selected auth list
    (0, react_1.useEffect)(() => {
        if (authDrawerOpen && applicationId && typeId) {
            if (typeof fetchAuthList === 'function')
                fetchAuthList({
                    applicationId,
                    type: 'folder',
                    typeId,
                });
        }
    }, [authDrawerOpen, applicationId, typeId, fetchAuthList]);
    // fetch project 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 handleFilterSelect = (0, react_1.useCallback)((appId) => {
        setSearch(appId);
        fetchList({
            organizationId,
            page: 1,
            size: pageInfo.size,
            search: appId,
        });
    }, [organizationId, pageInfo]);
    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,
                        },
                    ] }), style: {
                    maxWidth: type === 'projects' ? global_1.WIDTH_DEFAULT : '100%',
                    overflow: type === 'projects' ? 'unset' : 'hidden auto',
                } },
                react_1.default.createElement(Header, null,
                    type !== 'application' ? (react_1.default.createElement(index_1.ApplicationSelector, { list: applicationList, onSelect: handleFilterSelect })) : (react_1.default.createElement("div", null)),
                    type !== 'involved' && (react_1.default.createElement(antd_1.Button, { type: "primary", onClick: () => openDrawer(true) },
                        react_1.default.createElement(icons_1.PlusOutlined, null),
                        " ",
                        project.add))),
                react_1.default.createElement(index_2.List, { organizationId: organizationId, applicationId: applicationId, search: search, type: type, pageInfo: pageInfo, loading: loading, projectList: projectList, fetchProjectList: fetchList, deleteProject: deleteProject, openDrawer: openDrawer, openAuthDrawer: openAuthDrawer }))),
        react_1.default.createElement(index_2.EditDrawer, { saveLoading: saveLoading, organizationId: organizationId, applicationId: type === 'application' ? applicationId : undefined, type: type, pageInfo: pageInfo, drawerOpen: drawerOpen, editProject: editProject, apps: applicationList, fetchProjectList: fetchList, fetchApps: fetchApps, updateEditProject: updateEditProject, saveProject: saveProject, closeDrawer: openDrawer }),
        !isFromProject && (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 = ProjectFolderComponent;
