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/info2/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("../../../info/model");
var import_utils2 = require("../addService/utils");
var import_serviceItem = __toESM(require("../serviceItem"));
var ServiceList = ({ onEdit }) => {
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const handleEditService = (item) => {
    if (state.disabledEdit || state.disableEventBookingEdit) {
      return;
    }
    let cacheItem = (0, import_utils2.getProductExtend)({
      cacheItem: { ...item, new: 0 },
      state
    });
    dispatch({
      type: "setService",
      payload: {
        editModal: true,
        cacheItem
      }
    });
    onEdit == null ? void 0 : onEdit(cacheItem);
  };
  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 getErrorMessage = (0, import_react.useCallback)((service) => {
    let errorList = [];
    const list = Object.values(service._extend.resource || []);
    errorList = list.reduce((pre, cur) => {
      return pre.concat(
        (cur || []).reduce((preItem, curItem) => {
          return preItem.concat(curItem.errors || []);
        }, [])
      );
    }, []);
    let errorMessage = "";
    console.log(errorList, list, "cur.errors");
    if (errorList == null ? void 0 : errorList.length) {
      errorMessage = `${errorList.length} ${import_utils.locales.getText(
        "pisell2.text.warnings"
      )}`;
    }
    return errorMessage;
  }, []);
  const handleLike = (status, item, resource) => {
    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,
          resource: {
            ...d._extend.resource,
            [resource.form_id]: d._extend.resource[resource.form_id].map(
              (d2, i) => {
                if (d2.relation_id === resource.relation_id) {
                  return {
                    ...d2,
                    like_status: status
                  };
                }
                return d2;
              }
            )
          }
        }
      } : d
    );
    dispatch({
      type: "setService",
      payload: {
        value: _list,
        edit: true
      }
    });
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "" }, state.service.value.map((d) => {
    let errorMessage = getErrorMessage(d);
    return /* @__PURE__ */ import_react.default.createElement(
      import_serviceItem.default,
      {
        key: d._id,
        errorMessage,
        d,
        handleEditService,
        state,
        handleLike,
        handleDeleteService
      }
    );
  }));
};
var serviceList_default = ServiceList;
