function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React from 'react';
import { Dropdown } from 'antd';
import classNames from 'classnames';
import Check from '@pisell/icon/es/Check';
import ChevronDown from '@pisell/icon/es/ChevronDown';
import defaultLogo from "./defaultLogo.png";
import "./index.less";
var Logo = function Logo(_ref) {
  var logo = _ref.logo,
    className = _ref.className;
  return /*#__PURE__*/React.createElement("img", {
    src: logo || defaultLogo,
    alt: "logo",
    className: classNames('organization-tenant-switcher-item-logo', className)
  });
};
var OrganizationTenantSwitcher = function OrganizationTenantSwitcher(_ref2) {
  var onChange = _ref2.onChange,
    value = _ref2.value,
    options = _ref2.options,
    _ref2$showLogo = _ref2.showLogo,
    showLogo = _ref2$showLogo === void 0 ? true : _ref2$showLogo,
    _ref2$showName = _ref2.showName,
    showName = _ref2$showName === void 0 ? true : _ref2$showName,
    _ref2$readOnly = _ref2.readOnly,
    readOnly = _ref2$readOnly === void 0 ? false : _ref2$readOnly,
    onClick = _ref2.onClick;
  var getCurrentOrg = function getCurrentOrg() {
    return options.find(function (opt) {
      return (opt === null || opt === void 0 ? void 0 : opt.value) === value;
    });
  };
  var items = _toConsumableArray(options.map(function (opt) {
    return {
      key: opt === null || opt === void 0 ? void 0 : opt.value,
      label: /*#__PURE__*/React.createElement("div", {
        className: "organization-tenant-switcher-item"
      }, /*#__PURE__*/React.createElement(Logo, {
        logo: opt === null || opt === void 0 ? void 0 : opt.logo
      }), /*#__PURE__*/React.createElement("div", {
        className: "organization-tenant-switcher-item-name",
        title: opt === null || opt === void 0 ? void 0 : opt.label
      }, opt === null || opt === void 0 ? void 0 : opt.label), (opt === null || opt === void 0 ? void 0 : opt.value) === value && /*#__PURE__*/React.createElement(Check, {
        className: "organization-tenant-switcher-item-check"
      }))
    };
  }));
  var handleMenuClick = function handleMenuClick(e) {
    if (e.key !== 'create') {
      var selectedOption = options.find(function (opt) {
        return opt.value === e.key;
      });
      if (selectedOption) {
        onChange === null || onChange === void 0 ? void 0 : onChange(selectedOption.value, selectedOption);
      }
    }
  };
  var currentOrg = getCurrentOrg();
  return /*#__PURE__*/React.createElement(Dropdown, {
    overlayClassName: "organization-tenant-switcher-overlay",
    menu: {
      items: items,
      onClick: handleMenuClick,
      selectedKeys: [value],
      style: {
        maxHeight: 370,
        overflow: 'auto'
      }
    },
    trigger: readOnly ? [] : ['click'],
    disabled: readOnly
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('organization-tenant-switcher-switch', {
      'organization-tenant-switcher-switch-readonly': readOnly
    }),
    style: !showLogo ? {
      paddingLeft: 16
    } : {},
    onClick: onClick
  }, showLogo && /*#__PURE__*/React.createElement(Logo, {
    logo: currentOrg === null || currentOrg === void 0 ? void 0 : currentOrg.logo,
    className: "organization-tenant-switcher-switch-logo"
  }), /*#__PURE__*/React.createElement("div", {
    className: "organization-tenant-switcher-switch-name",
    title: currentOrg === null || currentOrg === void 0 ? void 0 : currentOrg.label
  }, showName && (currentOrg === null || currentOrg === void 0 ? void 0 : currentOrg.label)), !readOnly && /*#__PURE__*/React.createElement(ChevronDown, {
    className: "organization-tenant-switcher-switch-chevron"
  })));
};
export default OrganizationTenantSwitcher;