"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TypologyDropdown = void 0;

var _react = _interopRequireWildcard(require("react"));

var _async = _interopRequireDefault(require("react-select/async"));

var _reactSelect = require("react-select");

var _lodash = require("lodash");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Icon = require("../Icon");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["children"],
    _excluded2 = ["children"],
    _excluded3 = ["onSearch", "onSelect", "debounce", "multiple"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var prepareTopologies = function prepareTopologies(topologies) {
  return topologies.map(function (topology) {
    return _objectSpread(_objectSpread({}, topology), {}, {
      value: topology.id,
      label: topology.name
    });
  });
};

var ValueContainer = function ValueContainer(_ref) {
  var _props$selectProps;

  var children = _ref.children,
      props = _objectWithoutProperties(_ref, _excluded);

  var isMulti = props.isMulti;

  var _ref2 = (props === null || props === void 0 ? void 0 : props.selectProps) || {},
      inputValue = _ref2.inputValue;

  var _ref3 = (props === null || props === void 0 ? void 0 : (_props$selectProps = props.selectProps) === null || _props$selectProps === void 0 ? void 0 : _props$selectProps.value) || {},
      icon = _ref3.icon;

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.components.ValueContainer, _objectSpread(_objectSpread({}, props), {}, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "flex items-center",
      children: [!isMulti && !inputValue && icon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
        name: icon,
        size: "xl",
        className: "mr-1"
      }), children]
    })
  }));
};

var Option = function Option(_ref4) {
  var children = _ref4.children,
      props = _objectWithoutProperties(_ref4, _excluded2);

  var _props$data = props === null || props === void 0 ? void 0 : props.data,
      icon = _props$data.icon,
      status = _props$data.status;

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.components.Option, _objectSpread(_objectSpread({}, props), {}, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "flex w-full items-center",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
        name: icon,
        size: "xl",
        className: "mr-2"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "w-full",
        children: [" ", children]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "bg-blue-200 px-2 rounded",
        children: status
      })]
    })
  }));
};

var MultiValueLabel = function MultiValueLabel(props) {
  var icon = props.data.icon;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "flex px-1 items-center",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
      name: icon,
      size: "xl"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.components.MultiValueLabel, _objectSpread({}, props))]
  });
};

var TypologyDropdown = function TypologyDropdown(_ref5) {
  var onSearch = _ref5.onSearch,
      onSelect = _ref5.onSelect,
      debounceTime = _ref5.debounce,
      multiple = _ref5.multiple,
      props = _objectWithoutProperties(_ref5, _excluded3);

  var handleSearch = (0, _react.useRef)((0, _lodash.debounce)(function (name, callback) {
    onSearch(name).then(function (topologies) {
      callback(prepareTopologies(topologies));
    });
  }, debounceTime)).current;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_async.default, _objectSpread({
    cacheOptions: true,
    isClearable: true,
    defaultOptions: true,
    loadOptions: handleSearch,
    placeholder: "Input topology name...",
    components: {
      ValueContainer: ValueContainer,
      Option: Option,
      MultiValueLabel: MultiValueLabel
    },
    onChange: onSelect,
    isMulti: multiple
  }, props));
};

exports.TypologyDropdown = TypologyDropdown;
TypologyDropdown.propTypes = {
  onSearch: _propTypes.default.func.isRequired,
  onSelect: _propTypes.default.func.isRequired,
  debounce: _propTypes.default.number,
  multiple: _propTypes.default.bool
};
TypologyDropdown.defaultProps = {
  debounce: 500,
  multiple: false
};