import React from 'react';
import { Empty, Spin } from 'antd';
import { Icon, Input } from '@pisell/materials';
import { 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,
    onWorkAreaChange = _ref.onWorkAreaChange,
    onKeywordChange = _ref.onKeywordChange,
    onPickDevice = _ref.onPickDevice;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__connect-areas"
  }, /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__connect-section-title"
  }, getText('pisell2.saas.device-planning.connect.work-areas')), /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__connect-section-desc"
  }, getText('pisell2.saas.device-planning.connect.select-area')), /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__connect-area-list"
  }, workAreas.map(function (area) {
    return /*#__PURE__*/React.createElement("div", {
      key: area.id,
      role: "button",
      tabIndex: 0,
      className: "device-detail-drawer__connect-area".concat(selectedWorkAreaId === area.id ? ' is-active' : ''),
      onClick: function onClick() {
        return onWorkAreaChange(area.id);
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: "device-detail-drawer__connect-area-header"
    }, /*#__PURE__*/React.createElement("strong", null, translation(area.name) || area.id), area.id === currentWorkAreaId ? /*#__PURE__*/React.createElement("span", {
      className: "device-detail-drawer__connect-current"
    }, getText('pisell2.saas.device-planning.connect.current')) : null), /*#__PURE__*/React.createElement("span", null, getDeviceCountText(getWorkAreaDeviceCount(area))));
  }))), /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__connect-targets"
  }, /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__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: "device-detail-drawer__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: "device-detail-drawer__connect-device-list"
  }, /*#__PURE__*/React.createElement(Spin, {
    spinning: loading || Boolean(pickingDeviceId)
  }, /*#__PURE__*/React.createElement("div", {
    className: "device-detail-drawer__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('device-detail-drawer__connect-device-card', {
        'is-disabled': disabled || Boolean(pickingDeviceId)
      }),
      onClick: function onClick() {
        if (!disabled && !pickingDeviceId) {
          onPickDevice(device);
        }
      }
    }, /*#__PURE__*/React.createElement("span", {
      className: "device-detail-drawer__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: "device-detail-drawer__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;