function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import { Form, Radio } from 'antd';
import { Icon, Input, Select } from '@pisell/materials';
import { SettingOutlined, UsbOutlined, WifiOutlined } from '@ant-design/icons';
import { CONNECTION_TYPE_OPTIONS } from "../../utils";
var BluetoothIcon = function BluetoothIcon() {
  return /*#__PURE__*/React.createElement("svg", {
    viewBox: "0 0 1024 1024",
    width: "1em",
    height: "1em",
    fill: "currentColor",
    "aria-hidden": "true",
    focusable: "false"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M844.799642 674.803923 538.533306 521.15859l307.696917-177.029926c10.583033-5.006014 17.398252-15.59621 17.569144-27.302833s-6.332219-22.491247-16.764825-27.804253L475.584626 65.954131c-9.517771-4.846378-20.869307-4.39817-29.974685 1.181919-9.105379 5.581112-14.656815 15.493879-14.656815 26.174126l0 288.760636L210.662931 196.715267c-12.974499-10.915607-32.339546-9.248641-43.255153 3.724835-10.915607 12.973476-9.248641 32.339546 3.724835 43.255153L430.953126 462.312344l0 102.83511L171.558307 775.571954c-13.166881 10.68127-15.181771 30.013571-4.500501 43.180452 6.068206 7.480368 14.924921 11.360745 23.859407 11.360745 6.794753 0 13.634532-2.245134 19.322068-6.859221l220.714867-179.046862 0 283.065937c0 10.689457 5.561669 20.61041 14.680351 26.188452 4.903683 2.99931 10.455119 4.510734 16.019859 4.510734 4.78498 0 9.580193-1.118474 13.980409-3.368724l371.449917-224.085638c10.664897-5.456269 17.182334-16.618493 16.691147-28.589129C863.284645 689.959088 855.875908 679.368892 844.799642 674.803923zM763.458055 315.360458 492.351498 475.082182l0-21.60711c0.563842-3.368724 0.558725-6.801916 0-10.153244L492.351498 143.395898 763.458055 315.360458zM492.351498 877.084952 492.351498 584.489988c0.558725-3.347235 0.562818-6.777357 0-10.146081l0-5.809309 267.472797 137.65822L492.351498 877.084952z"
  }));
};
var CONNECT_CONNECTION_TYPE_OPTIONS = CONNECTION_TYPE_OPTIONS.filter(function (option) {
  return option.value !== 'serial';
}).map(function (option) {
  return _objectSpread(_objectSpread({}, option), {}, {
    icon: option.value === 'usb' ? /*#__PURE__*/React.createElement(UsbOutlined, null) : option.value === 'ethernet' ? /*#__PURE__*/React.createElement(Icon, {
      type: "ApiOutlined"
    }) : option.value === 'wifi' ? /*#__PURE__*/React.createElement(WifiOutlined, null) : option.value === 'bluetooth' ? /*#__PURE__*/React.createElement(BluetoothIcon, null) : null
  });
});
var IPV4_PATTERN = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
var needsNetworkConfig = function needsNetworkConfig(connectionType) {
  return connectionType === 'ethernet' || connectionType === 'wifi';
};
var validateRequired = function validateRequired(message) {
  return [{
    required: true,
    message: message
  }];
};
var ConnectionConfigFields = function ConnectionConfigFields(_ref) {
  var connectionType = _ref.connectionType,
    networkMethod = _ref.networkMethod,
    profileRequired = _ref.profileRequired,
    profileOptions = _ref.profileOptions,
    getText = _ref.getText;
  var validateIpAddress = function validateIpAddress(_rule, value) {
    var ipValue = String(value || '').trim();
    if (!ipValue || IPV4_PATTERN.test(ipValue)) {
      return Promise.resolve();
    }
    return Promise.reject(new Error(getText('pisell2.saas.device-planning.connect.ip-address')));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-peripheral-connect__section-title"
  }, /*#__PURE__*/React.createElement(SettingOutlined, null), getText('pisell2.saas.device-planning.connect.configuration')), /*#__PURE__*/React.createElement("div", {
    style: {
      height: '8px'
    }
  }), /*#__PURE__*/React.createElement(Form.Item, {
    name: "connection_type",
    label: getText('pisell2.saas.device-planning.connect.connection-type'),
    required: true,
    rules: validateRequired(getText('pisell2.saas.device-planning.connect.connection-type'))
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    className: "saas-app-peripheral-connect__option-grid"
  }, CONNECT_CONNECTION_TYPE_OPTIONS.map(function (option) {
    return /*#__PURE__*/React.createElement(Radio, {
      key: option.value,
      value: option.value,
      className: "saas-app-peripheral-connect__option"
    }, option.icon, option.label);
  }))), needsNetworkConfig(connectionType) ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, {
    name: "connection_method",
    label: getText('pisell2.saas.device-planning.connect.network-config'),
    required: true,
    rules: validateRequired(getText('pisell2.saas.device-planning.connect.network-config'))
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    className: "saas-app-peripheral-connect__option-grid"
  }, ['auto', 'manual'].map(function (method) {
    return /*#__PURE__*/React.createElement(Radio, {
      key: method,
      value: method,
      className: "saas-app-peripheral-connect__option"
    }, getText(method === 'auto' ? 'pisell2.saas.device-planning.connect.auto-discovery' : 'pisell2.saas.device-planning.connect.manual-ip'));
  }))), networkMethod === 'manual' ? /*#__PURE__*/React.createElement(Form.Item, {
    name: "connection_ip",
    label: getText('pisell2.saas.device-planning.connect.ip-address'),
    required: true,
    rules: [].concat(_toConsumableArray(validateRequired(getText('pisell2.saas.device-planning.connect.ip-address'))), [{
      validator: validateIpAddress
    }])
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "192.168.1.100"
  })) : null) : null, /*#__PURE__*/React.createElement(Form.Item, {
    name: "profile_ids",
    label: getText('pisell2.saas.device-planning.connect.peripheral-profiles'),
    required: profileRequired,
    rules: profileRequired ? [{
      type: 'array',
      required: true,
      min: 1,
      message: getText('pisell2.saas.device-planning.add.device-profile-required')
    }] : [],
    extra: /*#__PURE__*/React.createElement("div", {
      className: "saas-app-peripheral-connect__help"
    }, getText('pisell2.saas.device-planning.connect.profiles-help'))
  }, /*#__PURE__*/React.createElement(Select, {
    mode: "multiple",
    options: profileOptions,
    placeholder: getText('pisell2.saas.device-planning.connect.select-profiles')
  })));
};
export default ConnectionConfigFields;