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, { useCallback, useContext } from 'react';
import { locales } from '@pisell/utils';
import "./index.less";
import { Context } from "../../model";
import { getNoResource } from "../../../mock";
import { getErrorList } from "../../../utils";
import ServiceItem from "../serviceItem";
var ServiceList = function ServiceList(_ref) {
  var _state$service;
  var onEdit = _ref.onEdit;
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var errorTypes = {
    product: locales.getText('pisell2.text.status.service-error-1'),
    time: locales.getText('pisell2.text.status.service-error-2'),
    limit: locales.getText('pisell2.text.status.service-error-3'),
    maxLimit: locales.getText('pisell2.text.status.service-error-3')
  };
  var handleEditService = function handleEditService(item) {
    var _window$openBookingEd, _window;
    if (state.disabledEdit || state.disableEventBookingEdit) {
      return;
    }
    (_window$openBookingEd = (_window = window).openBookingEditModal) === null || _window$openBookingEd === void 0 || _window$openBookingEd.call(_window);
    dispatch({
      type: 'setService',
      payload: {
        editModal: true,
        cacheItem: _objectSpread(_objectSpread({}, item), {}, {
          new: 0
        })
      }
    });
    onEdit === null || onEdit === void 0 || onEdit(_objectSpread(_objectSpread({}, item), {}, {
      new: 0
    }));
  };
  var handleDeleteService = function handleDeleteService(item) {
    var _window$closeBookingE, _window2;
    if (state.disabledEdit || state.disableEventBookingEdit || state.channelDisabledEdit) {
      return;
    }
    var _list = _toConsumableArray(state.service.value);
    _list = _list.filter(function (d) {
      return d._id !== item._id;
    });
    (_window$closeBookingE = (_window2 = window).closeBookingEditModal) === null || _window$closeBookingE === void 0 || _window$closeBookingE.call(_window2);
    dispatch({
      type: 'setService',
      payload: {
        editModal: false,
        value: _list,
        edit: true,
        cacheItem: null
      }
    });
  };
  var getResourceItem = useCallback(function (id) {
    if (id === 0) {
      return getNoResource();
    }
    return state.service.resources.find(function (d) {
      return d.form_record_id === id;
    });
  }, [state.service.resources]);
  var getErrorMessage = useCallback(function (service, resourceItem) {
    var errorList = getErrorList(resourceItem, service);
    var errorMessage = '';
    if (!service.id) {
      return errorList[0].label;
    }
    if (errorList.length) {
      if (errorList.length > 1) {
        errorMessage = "".concat(errorList.length, " ").concat(locales.getText('pisell2.text.warnings'));
      } else {
        errorMessage = (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.labelText) + ' ' + errorTypes[errorList[0].type];
      }
    }
    return errorMessage;
  }, []);
  var handleLike = function handleLike(status, item) {
    if (state.disabledEdit) {
      return;
    }
    var _list = _toConsumableArray(state.service.value);
    var _id = item === null || item === void 0 ? void 0 : item._id;
    _list = _list.map(function (d) {
      return d._id === _id ? _objectSpread(_objectSpread({}, d), {}, {
        _extend: _objectSpread(_objectSpread({}, d._extend), {}, {
          like_status: status
        })
      }) : d;
    });
    dispatch({
      type: 'setService',
      payload: {
        value: _list,
        edit: true
      }
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: ""
  }, (_state$service = state.service) === null || _state$service === void 0 || (_state$service = _state$service.value) === null || _state$service === void 0 ? void 0 : _state$service.map(function (d) {
    var _d$_extend;
    var resourceItem = getResourceItem((_d$_extend = d._extend) === null || _d$_extend === void 0 ? void 0 : _d$_extend.resource);
    var errorMessage = getErrorMessage(d, resourceItem);
    return /*#__PURE__*/React.createElement(ServiceItem, {
      key: d._id,
      getResourceItem: getResourceItem,
      errorMessage: errorMessage,
      resourceItem: resourceItem,
      d: d,
      handleEditService: handleEditService,
      state: state,
      handleLike: handleLike,
      handleDeleteService: handleDeleteService
    });
  }));
};
export default ServiceList;