"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_redux_1 = require("react-redux");
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 system_1 = require("@/pages/system");
const ACTIONS = __importStar(require("@/store/actions/applications/detail/settings/application"));
const dynamics_1 = __importDefault(require("./dynamics"));
const file_1 = __importDefault(require("./file"));
const overview_1 = __importDefault(require("./overview"));
const packages_1 = __importDefault(require("./packages"));
const projects_1 = __importDefault(require("./projects"));
const resources_1 = __importDefault(require("./resources"));
const settings_1 = __importDefault(require("./settings"));
const AppName = styled_components_1.default.div `
  line-height: 48px;
  height: 48px;
  display: inline-block;
  width: 100%;
  color: rgba(0, 0, 0, 0.54);
  padding-left: 24px;
  border-bottom: 1px solid rgb(221, 221, 221);
  background: #fff;
  border-right: 1px solid #f0f0f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
`;
const { Sider } = antd_1.Layout;
const mapStateToProps = (store) => ({
    application: store.applications.detail.settings.app.application,
});
const mapDispatchToProps = {
    fetchAppDetail: ACTIONS.fetchApplicationInfo,
    updateApplicationId: ACTIONS.updateApplicationId,
};
const ApplicationDetail = (props) => {
    const { application, fetchAppDetail, updateApplicationId } = props;
    const [siderCollapsed, setSiderCollapsed] = (0, react_1.useState)(false);
    const [selectedKeys, setSelectedKeys] = (0, react_1.useState)(['files']);
    // url params
    const history = (0, react_router_dom_1.useHistory)();
    const location = (0, react_router_dom_1.useLocation)();
    const routeMatch = (0, react_router_dom_1.useRouteMatch)();
    const { applicationId } = (0, react_router_dom_1.useParams)();
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file, setting } = locale.business;
    (0, react_1.useEffect)(() => {
        if (applicationId) {
            // fetch detail info
            fetchAppDetail(applicationId);
            // push to store
            updateApplicationId(applicationId);
        }
    }, []);
    // generate menu key with url search params
    (0, react_1.useEffect)(() => {
        setSelectedKeys(location.pathname
            .replace(routeMatch.url, '')
            .replace('/', '')
            .split('/')
            .filter((item) => !!item));
    }, [location]);
    const handleClick = (e) => {
        const pathname = `/applications/${applicationId}/${e.keyPath.reverse().join('/')}`;
        history.push({
            pathname,
        });
        // update menu key
        setSelectedKeys(e.keyPath);
    };
    return (react_1.default.createElement(antd_1.Layout, { hasSider: true, style: { height: '100%' } },
        react_1.default.createElement(Sider, { theme: "light", width: 250, collapsedWidth: 60, breakpoint: "xl", onBreakpoint: (broken) => setSiderCollapsed(broken), style: { height: '100%', overflow: 'auto' } },
            react_1.default.createElement(AppName, { style: { paddingLeft: siderCollapsed ? 8 : 24 } }, application === null || application === void 0 ? void 0 : application.name),
            react_1.default.createElement(antd_1.Menu, { onClick: handleClick, mode: "inline", selectedKeys: selectedKeys, defaultOpenKeys: ['file', 'settings', 'builder'], theme: "light" },
                react_1.default.createElement(antd_1.Menu.Item, { key: "overview", icon: react_1.default.createElement(icons_1.DashboardOutlined, null) }, global.overview),
                react_1.default.createElement(antd_1.Menu.Item, { key: "projects", icon: react_1.default.createElement(icons_1.ProjectOutlined, null) }, global.project),
                react_1.default.createElement(antd_1.Menu.SubMenu, { key: "file", title: file.name, icon: react_1.default.createElement(icons_1.FileSearchOutlined, null) },
                    react_1.default.createElement(antd_1.Menu.Item, { key: "pages", icon: react_1.default.createElement(icons_1.FileTextOutlined, null) }, file.page),
                    react_1.default.createElement(antd_1.Menu.Item, { key: "templates", icon: react_1.default.createElement(icons_1.FileOutlined, null) }, file.template),
                    react_1.default.createElement(antd_1.Menu.Item, { key: "functions", icon: react_1.default.createElement(icons_1.FunctionOutlined, null) }, global.functions),
                    react_1.default.createElement(antd_1.Menu.Item, { key: "variables", icon: react_1.default.createElement(icons_1.SlidersOutlined, null) }, global.variables),
                    react_1.default.createElement(antd_1.Menu.Item, { key: "conditions", icon: react_1.default.createElement(icons_1.ControlOutlined, null) }, global.conditions)),
                react_1.default.createElement(antd_1.Menu.Item, { key: "packages", icon: react_1.default.createElement(icons_1.BookOutlined, null) }, global.packages),
                react_1.default.createElement(antd_1.Menu.Item, { key: "resources", icon: react_1.default.createElement(icons_1.ContainerOutlined, null) }, global.resources),
                react_1.default.createElement(antd_1.Menu.Item, { key: "dynamics", icon: react_1.default.createElement(icons_1.BranchesOutlined, null) }, global.dynamics),
                react_1.default.createElement(antd_1.Menu.SubMenu, { key: "settings", title: global.setting, icon: react_1.default.createElement(icons_1.SettingOutlined, null) },
                    react_1.default.createElement(antd_1.Menu.SubMenu, { key: "builder", title: global.builder, icon: react_1.default.createElement(icons_1.BuildOutlined, null) },
                        react_1.default.createElement(antd_1.Menu.Item, { key: "component", icon: react_1.default.createElement(icons_1.BarsOutlined, null) }, setting.componentBar),
                        react_1.default.createElement(antd_1.Menu.Item, { key: "page", icon: react_1.default.createElement(icons_1.FileTextOutlined, null) }, file.page),
                        react_1.default.createElement(antd_1.Menu.Item, { key: "template", icon: react_1.default.createElement(icons_1.FileOutlined, null) }, file.template)),
                    react_1.default.createElement(antd_1.Menu.Item, { key: "application", icon: react_1.default.createElement(icons_1.AppstoreOutlined, null) }, global.application)))),
        react_1.default.createElement(react_router_dom_1.Switch, null,
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/overview", component: overview_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/file", component: file_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/packages", component: packages_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/projects", component: projects_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/resources", component: resources_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/dynamics", component: dynamics_1.default }),
            react_1.default.createElement(react_router_dom_1.Route, { path: "/applications/:applicationId/settings", component: settings_1.default }),
            react_1.default.createElement(react_router_dom_1.Redirect, { from: "/*", to: `/applications/${applicationId}/file/pages/list` }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ApplicationDetail);
