"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrganizationList = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const OrganizationGroup_1 = require("../OrganizationGroup");
const OrganizationItem_1 = require("../OrganizationItem");
const OrganizationList = (props) => {
    const { currentOrganization, currentOrganizationGroupLabel = 'Текущая организация', onSelectOrganization, organizations, groupBy, isShowCurrentOrganization, isHidePersonalData, renderItem, hasSearch = false, } = props;
    const getRenderedItem = (organization, options) => {
        if (renderItem) {
            return renderItem({
                organization,
                isSelected: options.isSelected,
                onSelect: options.onSelect,
            });
        }
        return ((0, jsx_runtime_1.jsx)(OrganizationItem_1.OrganizationItem, { ...organization, selected: options.isSelected, onClick: options.onSelect, isHidePersonalData: isHidePersonalData }));
    };
    // Показываем список организаций если:
    // - Есть поисковый запрос И есть организации
    // - Или нет поиска И организаций больше одной
    const shouldShowOrganizationsList = (hasSearch && organizations.length > 0) ||
        (!hasSearch && organizations.length > 1);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isShowCurrentOrganization && currentOrganization && ((0, jsx_runtime_1.jsx)(OrganizationGroup_1.OrganizationGroup, { label: currentOrganizationGroupLabel, children: getRenderedItem(currentOrganization, {
                    isSelected: true,
                }) })), shouldShowOrganizationsList &&
                (groupBy ? (groupBy.map(({ name, filter }) => {
                    const filteredOrganizations = filter(organizations);
                    return (filteredOrganizations.length > 0 && ((0, jsx_runtime_1.jsx)(OrganizationGroup_1.OrganizationGroup, { label: name, children: filteredOrganizations.map((organization) => ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: getRenderedItem(organization, {
                                isSelected: false,
                                onSelect: onSelectOrganization(organization),
                            }) }, organization.id))) }, name)));
                })) : ((0, jsx_runtime_1.jsx)(OrganizationGroup_1.OrganizationGroup, { label: "\u0412\u0441\u0435 \u043E\u0440\u0433\u0430\u043D\u0438\u0437\u0430\u0446\u0438\u0438", children: organizations.map((organization) => ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: getRenderedItem(organization, {
                            isSelected: false,
                            onSelect: onSelectOrganization(organization),
                        }) }, organization.id))) })))] }));
};
exports.OrganizationList = OrganizationList;
