var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/info/service/serviceList/index.tsx
var serviceList_exports = {};
__export(serviceList_exports, {
  default: () => serviceList_default
});
module.exports = __toCommonJS(serviceList_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var import_model = require("../../model");
var import_mock = require("../../../mock");
var import_utils2 = require("../../../utils");
var import_serviceItem = __toESM(require("../serviceItem"));
var ServiceList = ({ onEdit }) => {
  var _a, _b;
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const errorTypes = {
    product: import_utils.locales.getText("pisell2.text.status.service-error-1"),
    time: import_utils.locales.getText("pisell2.text.status.service-error-2"),
    limit: import_utils.locales.getText("pisell2.text.status.service-error-3"),
    maxLimit: import_utils.locales.getText("pisell2.text.status.service-error-3")
  };
  const handleEditService = (item) => {
    if (state.disabledEdit || state.disableEventBookingEdit) {
      return;
    }
    dispatch({
      type: "setService",
      payload: {
        editModal: true,
        cacheItem: { ...item, new: 0 }
      }
    });
    onEdit == null ? void 0 : onEdit({ ...item, new: 0 });
  };
  const handleDeleteService = (item) => {
    if (state.disabledEdit || state.disableEventBookingEdit || state.channelDisabledEdit) {
      return;
    }
    let _list = [...state.service.value];
    _list = _list.filter((d) => d._id !== item._id);
    dispatch({
      type: "setService",
      payload: {
        editModal: false,
        value: _list,
        edit: true,
        cacheItem: null
      }
    });
  };
  const getResourceItem = (0, import_react.useCallback)(
    (id) => {
      if (id === 0) {
        return (0, import_mock.getNoResource)();
      }
      return state.service.resources.find((d) => d.form_record_id === id);
    },
    [state.service.resources]
  );
  const getErrorMessage = (0, import_react.useCallback)((service, resourceItem) => {
    const errorList = (0, import_utils2.getErrorList)(resourceItem, service);
    let errorMessage = "";
    if (!service.id) {
      return errorList[0].label;
    }
    if (errorList.length) {
      if (errorList.length > 1) {
        errorMessage = `${errorList.length} ${import_utils.locales.getText(
          "pisell2.text.warnings"
        )}`;
      } else {
        errorMessage = (resourceItem == null ? void 0 : resourceItem.labelText) + " " + errorTypes[errorList[0].type];
      }
    }
    return errorMessage;
  }, []);
  const handleLike = (status, item) => {
    if (state.disabledEdit) {
      return;
    }
    let _list = [...state.service.value];
    let _id = item == null ? void 0 : item._id;
    _list = _list.map(
      (d) => d._id === _id ? {
        ...d,
        _extend: {
          ...d._extend,
          like_status: status
        }
      } : d
    );
    dispatch({
      type: "setService",
      payload: {
        value: _list,
        edit: true
      }
    });
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, (_b = (_a = state.service) == null ? void 0 : _a.value) == null ? void 0 : _b.map((d) => {
    var _a2;
    const resourceItem = getResourceItem((_a2 = d._extend) == null ? void 0 : _a2.resource);
    let errorMessage = getErrorMessage(d, resourceItem);
    return /* @__PURE__ */ import_react.default.createElement(
      import_serviceItem.default,
      {
        key: d._id,
        getResourceItem,
        errorMessage,
        resourceItem,
        d,
        handleEditService,
        state,
        handleLike,
        handleDeleteService
      }
    );
  }));
};
var serviceList_default = ServiceList;
