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, { useState } from 'react';
import { Empty, Spin } from 'antd';
import { Icon, Input, PisellModal } from '@pisell/materials';
import { DownOutlined, SearchOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { DEVICE_ICON_COLOR } from "../../utils";
var PickExistingDeviceStep = function PickExistingDeviceStep(_ref) {
  var _workAreas$find;
  var workAreas = _ref.workAreas,
    selectedWorkAreaId = _ref.selectedWorkAreaId,
    currentWorkAreaId = _ref.currentWorkAreaId,
    filteredDevices = _ref.filteredDevices,
    connectedDeviceIds = _ref.connectedDeviceIds,
    loading = _ref.loading,
    pickingDeviceId = _ref.pickingDeviceId,
    keyword = _ref.keyword,
    targetKind = _ref.targetKind,
    getText = _ref.getText,
    translation = _ref.translation,
    getWorkAreaDeviceCount = _ref.getWorkAreaDeviceCount,
    getDeviceCountText = _ref.getDeviceCountText,
    isPhone = _ref.isPhone,
    onWorkAreaChange = _ref.onWorkAreaChange,
    onKeywordChange = _ref.onKeywordChange,
    onPickDevice = _ref.onPickDevice;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    workAreaPickerOpen = _useState2[0],
    setWorkAreaPickerOpen = _useState2[1];
  var selectedWorkArea = workAreas.find(function (area) {
    return area.id === selectedWorkAreaId;
  });
  var renderWorkAreaList = function renderWorkAreaList() {
    var closeAfterSelect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    return /*#__PURE__*/React.createElement("div", {
      className: "saas-app-peripheral-connect__area-list"
    }, workAreas.map(function (area) {
      return /*#__PURE__*/React.createElement("div", {
        key: area.id,
        role: "button",
        tabIndex: 0,
        className: "saas-app-peripheral-connect__area".concat(selectedWorkAreaId === area.id ? ' is-active' : ''),
        onClick: function onClick() {
          onWorkAreaChange(area.id);
          if (closeAfterSelect) {
            setWorkAreaPickerOpen(false);
          }
        }
      }, /*#__PURE__*/React.createElement("div", {
        className: "saas-app-peripheral-connect__area-header"
      }, /*#__PURE__*/React.createElement("strong", null, translation(area.name) || area.id), area.id === currentWorkAreaId ? /*#__PURE__*/React.createElement("span", {
        className: "saas-app-peripheral-connect__current"
      }, getText('pisell2.saas.device-planning.connect.current')) : null), /*#__PURE__*/React.createElement("span", null, getDeviceCountText(getWorkAreaDeviceCount(area))));
    }));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__areas"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__section-title"
  }, getText('pisell2.saas.device-planning.connect.work-areas')), /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__section-desc"
  }, getText('pisell2.saas.device-planning.connect.select-area')), isPhone ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "saas-app-peripheral-connect__workarea-trigger",
    onClick: function onClick() {
      return setWorkAreaPickerOpen(true);
    }
  }, /*#__PURE__*/React.createElement("span", null, translation(selectedWorkArea === null || selectedWorkArea === void 0 ? void 0 : selectedWorkArea.name) || (selectedWorkArea === null || selectedWorkArea === void 0 ? void 0 : selectedWorkArea.id) || '-'), /*#__PURE__*/React.createElement(DownOutlined, null)), /*#__PURE__*/React.createElement(PisellModal, {
    open: workAreaPickerOpen,
    title: getText('pisell2.saas.device-planning.connect.work-areas'),
    footer: null,
    width: "100%",
    platform: "h5",
    mobileModalHeight: "auto",
    className: "saas-app-peripheral-workarea-picker-modal",
    onCancel: function onCancel() {
      return setWorkAreaPickerOpen(false);
    },
    bodyStyle: {
      padding: '24px 10px 24px 24px'
    }
  }, renderWorkAreaList(true))) : renderWorkAreaList()), /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__targets"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__workarea-name"
  }, translation((_workAreas$find = workAreas.find(function (area) {
    return area.id === selectedWorkAreaId;
  })) === null || _workAreas$find === void 0 ? void 0 : _workAreas$find.name)), /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__filters"
  }, /*#__PURE__*/React.createElement(Input, {
    allowClear: true,
    prefix: /*#__PURE__*/React.createElement(SearchOutlined, null),
    value: keyword,
    placeholder: getText('pisell2.saas.device-planning.connect.search'),
    onChange: function onChange(event) {
      return onKeywordChange(event.target.value);
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__device-list"
  }, /*#__PURE__*/React.createElement(Spin, {
    spinning: loading || Boolean(pickingDeviceId)
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__device-grid"
  }, filteredDevices.map(function (device) {
    var _device$type, _device$type2;
    var disabled = connectedDeviceIds.includes(device.id);
    return /*#__PURE__*/React.createElement("div", {
      key: device.id,
      role: "button",
      tabIndex: disabled || pickingDeviceId ? -1 : 0,
      "aria-disabled": disabled || Boolean(pickingDeviceId),
      className: classNames('saas-app-peripheral-connect__device-card', {
        'is-disabled': disabled || Boolean(pickingDeviceId)
      }),
      onClick: function onClick() {
        if (!disabled && !pickingDeviceId) {
          onPickDevice(device);
        }
      }
    }, /*#__PURE__*/React.createElement("span", {
      className: "saas-app-peripheral-connect__device-icon is-".concat(device.type_kind)
    }, /*#__PURE__*/React.createElement(Icon, {
      type: ((_device$type = device.type) === null || _device$type === void 0 ? void 0 : _device$type.icon) || 'DesktopOutlined',
      color: DEVICE_ICON_COLOR[device.type_kind]
    })), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("strong", null, translation(device.name) || device.id), /*#__PURE__*/React.createElement("span", null, translation((_device$type2 = device.type) === null || _device$type2 === void 0 ? void 0 : _device$type2.name) || device.type_code)));
  })), !filteredDevices.length ? /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__empty"
  }, /*#__PURE__*/React.createElement(Empty, {
    image: Empty.PRESENTED_IMAGE_SIMPLE,
    description: getText(targetKind === 'smart' ? 'pisell2.saas.device-planning.connect.no-smart' : 'pisell2.saas.device-planning.connect.no-peripherals')
  })) : null))));
};
export default PickExistingDeviceStep;