"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_redux_1 = require("react-redux");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/workspace/applications/list"));
const components_1 = require("@/pages/components");
const system_1 = require("@/pages/system");
const components_2 = require("./components");
const PAGE_NUM = 1;
const PAGE_SIZE = 999;
const mapStateToProps = (state) => ({
    organizationId: state.system.user.organizationId,
    pageInfo: state.workspace.applications.list.pageInfo,
    drawerVisible: state.workspace.applications.list.editDrawerVisible,
    editApp: state.workspace.applications.list.editApp,
    authDrawerVisible: state.workspace.applications.list.authListDrawerVisible,
    authLoading: state.workspace.applications.list.authListLoading,
    authList: state.workspace.applications.list.authList,
    userList: state.workspace.applications.list.userList,
});
const mapDispatchToProps = {
    fetchList: ACTIONS.fetchList,
    saveApp: ACTIONS.saveApp,
    openDrawer: ACTIONS.openEditDrawer,
    openAuthDrawer: ACTIONS.updateAuthDrawerVisible,
    fetchAuthList: ACTIONS.fetchAuthList,
    fetchUserList: ACTIONS.fetchUserList,
    saveUser: ACTIONS.saveAuthUser,
    deleteUser: ACTIONS.deleteAuthUser,
};
function Application(props) {
    const { organizationId, pageInfo, editApp, authDrawerVisible, authLoading, authList, userList, fetchList, openDrawer, openAuthDrawer, fetchAuthList, fetchUserList, saveUser, deleteUser, } = props;
    const [typeId, setTypeId] = (0, react_1.useState)('');
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { application } = locale.business;
    (0, react_1.useEffect)(() => {
        fetchList({ organizationId, type: 'user', page: pageInfo.page, size: pageInfo.size });
    }, [fetchList, organizationId]);
    (0, react_1.useEffect)(() => {
        const newTypeId = editApp === null || editApp === void 0 ? void 0 : editApp.id;
        if (newTypeId)
            setTypeId(newTypeId);
    }, [editApp]);
    // fetch application selected auth list
    (0, react_1.useEffect)(() => {
        if (authDrawerVisible && typeId) {
            fetchAuthList({
                applicationId: typeId,
                type: 'application',
                typeId,
            });
        }
    }, [authDrawerVisible, typeId, fetchAuthList]);
    // fetch application selected authorize user available list
    (0, react_1.useEffect)(() => {
        if (authDrawerVisible) {
            fetchUserList({
                page: PAGE_NUM,
                size: PAGE_SIZE,
            });
        }
    }, [authDrawerVisible, fetchUserList]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(components_1.Content, null,
            react_1.default.createElement(components_1.FoxPageContent, { breadcrumb: react_1.default.createElement(components_1.FoxPageBreadcrumb, { breadCrumb: [
                        {
                            name: application.name,
                        },
                    ] }) },
                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),
                        " ",
                        application.add)),
                react_1.default.createElement(components_2.List, null))),
        react_1.default.createElement(components_2.EditDrawer, null),
        react_1.default.createElement(components_1.AuthorizeDrawer, { type: "application", typeId: typeId, applicationId: typeId, visible: authDrawerVisible, loading: authLoading, list: authList, users: userList, onClose: openAuthDrawer, onFetch: fetchAuthList, onAdd: saveUser, onDelete: deleteUser })));
}
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Application);
