"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 ACTIONS = __importStar(require("@/actions/system/user"));
const index_1 = require("@/utils/index");
const MenuGroupTitle = styled_components_1.default.a `
  display: flex;
  align-items: center;
  padding: 0 12px;
  line-height: 48px;
  cursor: pointer;
  color: #5b6b73;
  &:hover {
    color: #5b6b73;
  }
`;
const MenuItem = styled_components_1.default.div `
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 150px;
  padding: 0 8px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
  &.active {
    color: rgb(41, 141, 248);
    background: rgb(242, 248, 255);
  }
  &:hover {
    color: rgb(41, 141, 248);
    background: rgb(247, 247, 247);
  }
  &.no-select {
    &:hover {
      color: inherit;
      background: inherit;
    }
  }
`;
const mapStateToProps = (state) => ({
    list: state.system.user.organizations,
});
const mapDispatchToProps = {
    fetchList: ACTIONS.fetchOrganizationList,
    updateOrganizationId: ACTIONS.updateOrganizationId,
};
function OrganizationSelector(props) {
    const { list, fetchList, updateOrganizationId } = props;
    const [currOrg, setCurrOrg] = (0, react_1.useState)('');
    const { token, userInfo, languagePrefer, organizationPrefer } = (0, index_1.getLoginUser)();
    const organizationId = userInfo === null || userInfo === void 0 ? void 0 : userInfo.organizationId;
    (0, react_1.useEffect)(() => {
        fetchList();
    }, [fetchList]);
    (0, react_1.useEffect)(() => {
        const defaultOrg = list && list.length > 0 && organizationPrefer
            ? list.find((item) => item.id === organizationPrefer)
            : list.find((item) => item.default) || (list === null || list === void 0 ? void 0 : list[0]);
        if (defaultOrg && (defaultOrg === null || defaultOrg === void 0 ? void 0 : defaultOrg.id) === organizationId) {
            setCurrOrg((defaultOrg === null || defaultOrg === void 0 ? void 0 : defaultOrg.name) || '');
        }
        else {
            handleMenuClick({
                key: defaultOrg === null || defaultOrg === void 0 ? void 0 : defaultOrg.name,
            });
        }
    }, [list]);
    const handleMenuClick = (0, react_1.useCallback)((item) => {
        var _a;
        setCurrOrg(item.key);
        const newOrganizationId = list && ((_a = list.find((org) => org.name === item.key)) === null || _a === void 0 ? void 0 : _a.id);
        if (newOrganizationId) {
            (0, index_1.setLoginUser)({
                token,
                userInfo: Object.assign(Object.assign({}, userInfo), { organizationId: newOrganizationId }),
                languagePrefer,
                organizationPrefer: newOrganizationId,
            });
            // push new organization id to store
            updateOrganizationId(newOrganizationId);
        }
    }, [list, updateOrganizationId]);
    const menu = (react_1.default.createElement(antd_1.Menu, { onClick: handleMenuClick }, list &&
        list.map((item) => (react_1.default.createElement(antd_1.Menu.Item, { key: item.name },
            react_1.default.createElement(react_router_dom_1.Link, { to: {
                    pathname: '/workspace/application',
                } },
                react_1.default.createElement(MenuItem, null, item.name)))))));
    return (react_1.default.createElement(antd_1.Dropdown, { overlay: menu, trigger: ['click'] },
        react_1.default.createElement(MenuGroupTitle, null,
            currOrg,
            react_1.default.createElement(icons_1.CaretDownOutlined, { style: { fontSize: 8, marginLeft: 4 } }))));
}
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(OrganizationSelector);
