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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useMemo, useState } from 'react';
import { Empty, Input, Select } from '@pisell/materials';
import { SearchOutlined } from '@ant-design/icons';
import { DEVICE_KIND_LABEL_KEY, normalizeText } from "../../constants";
import { CONNECTION_TYPE_OPTIONS, getBrandCodeFromOptionValue, getBrandOptionValue, getConnectionTypeLabel, getModelConnectionTypes } from "../../utils";
import ModelPickerGrid from "./ModelPickerGrid";
var DeviceModelPicker = function DeviceModelPicker(_ref) {
  var activeKind = _ref.activeKind,
    models = _ref.models,
    _ref$brands = _ref.brands,
    brands = _ref$brands === void 0 ? [] : _ref$brands,
    getText = _ref.getText,
    translation = _ref.translation,
    onSelect = _ref.onSelect;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    keyword = _useState2[0],
    setKeyword = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    brandFilter = _useState4[0],
    setBrandFilter = _useState4[1];
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    connectionFilter = _useState6[0],
    setConnectionFilter = _useState6[1];
  var isPeripheral = activeKind === 'dumb';
  var brandNameMap = useMemo(function () {
    var map = new Map();
    brands.forEach(function (brand) {
      map.set(brand.code, translation(brand.name) || brand.code);
    });
    models.forEach(function (model) {
      var _model$brand;
      if (model.brand_code && (_model$brand = model.brand) !== null && _model$brand !== void 0 && _model$brand.name && !map.has(model.brand_code)) {
        map.set(model.brand_code, translation(model.brand.name) || model.brand_code);
      }
    });
    return map;
  }, [brands, models, translation]);
  var brandOptions = useMemo(function () {
    return brands.map(function (brand) {
      return {
        label: translation(brand.name) || brand.code,
        value: getBrandOptionValue(brand)
      };
    });
  }, [brands, translation]);
  var filteredModels = useMemo(function () {
    var value = normalizeText(keyword);
    return models.filter(function (model) {
      var name = translation(model.name);
      var connections = getModelConnectionTypes(model);
      var matchesKeyword = value ? normalizeText(name).includes(value) : true;
      var matchesBrand = brandFilter ? model.brand_code === getBrandCodeFromOptionValue(brandFilter) : true;
      var matchesConnection = connectionFilter ? connections.some(function (connection) {
        return normalizeText(connection) === connectionFilter;
      }) : true;
      return matchesKeyword && matchesBrand && matchesConnection;
    });
  }, [brandFilter, connectionFilter, keyword, models, translation]);
  var groupedModels = useMemo(function () {
    var groups = new Map();
    filteredModels.forEach(function (model) {
      var key = model.type_code || getText(DEVICE_KIND_LABEL_KEY[activeKind]);
      groups.set(key, [].concat(_toConsumableArray(groups.get(key) || []), [model]));
    });
    return Array.from(groups.entries());
  }, [activeKind, filteredModels, getText]);
  var renderModelMeta = function renderModelMeta(model) {
    if (!isPeripheral) {
      return /*#__PURE__*/React.createElement("div", {
        className: "add-device-modal__model-desc"
      }, translation(model.description) || '');
    }
    var brandName = model.brand_code ? brandNameMap.get(model.brand_code) || model.brand_code : '';
    var connections = getModelConnectionTypes(model);
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "add-device-modal__model-brand"
    }, brandName), /*#__PURE__*/React.createElement("div", {
      className: "add-device-modal__model-desc"
    }, translation(model.description) || ''), connections.length ? /*#__PURE__*/React.createElement("div", {
      className: "add-device-modal__connection-list"
    }, connections.map(function (connection) {
      return /*#__PURE__*/React.createElement("span", {
        key: connection,
        className: "add-device-modal__connection-tag"
      }, getConnectionTypeLabel(connection));
    })) : null);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "add-device-modal__body"
  }, /*#__PURE__*/React.createElement("div", {
    className: "add-device-modal__filters"
  }, /*#__PURE__*/React.createElement(Input, {
    allowClear: true,
    prefix: /*#__PURE__*/React.createElement(SearchOutlined, null),
    value: keyword,
    placeholder: getText('pisell2.saas.device-planning.add.search'),
    onChange: function onChange(event) {
      return setKeyword(event.target.value);
    },
    className: "add-device-modal__search"
  }), isPeripheral ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, {
    allowClear: true,
    value: brandFilter,
    placeholder: getText('pisell2.saas.device-planning.add.all-brands'),
    options: brandOptions,
    onChange: function onChange(value) {
      return setBrandFilter(value);
    },
    className: "add-device-modal__filter-select"
  }), /*#__PURE__*/React.createElement(Select, {
    allowClear: true,
    value: connectionFilter,
    placeholder: getText('pisell2.saas.device-planning.add.all-connections'),
    options: CONNECTION_TYPE_OPTIONS,
    onChange: function onChange(value) {
      return setConnectionFilter(value);
    },
    className: "add-device-modal__filter-select"
  })) : null), /*#__PURE__*/React.createElement("div", {
    className: "add-device-modal__model-list"
  }, groupedModels.length ? /*#__PURE__*/React.createElement(ModelPickerGrid, {
    groups: groupedModels,
    classNamePrefix: "add-device-modal",
    groupTitleClassName: "add-device-modal__group-title",
    translation: translation,
    onSelect: onSelect,
    renderMeta: renderModelMeta,
    useButton: false
  }) : /*#__PURE__*/React.createElement(Empty, {
    image: Empty.PRESENTED_IMAGE_SIMPLE,
    description: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, getText('pisell2.saas.device-planning.add.no-devices')), /*#__PURE__*/React.createElement("div", null, getText('pisell2.saas.device-planning.add.adjust-search')))
  })));
};
export default DeviceModelPicker;