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/service2/utils.ts
var utils_exports = {};
__export(utils_exports, {
  disabledDate: () => disabledDate,
  formItems: () => formItems,
  formatActiveKey: () => formatActiveKey,
  formatDateTime: () => formatDateTime,
  formatDayDate: () => formatDayDate,
  formatDayTime: () => formatDayTime,
  formatHolderLists: () => formatHolderLists,
  formatProducts: () => formatProducts,
  getAppointmentService: () => getAppointmentService,
  getDayProducts: () => getDayProducts,
  getErrorMessage: () => getErrorMessage,
  getIsSubjectRequire: () => getIsSubjectRequire,
  getPets: () => getPets,
  getResourceItem: () => getResourceItem,
  handleChangeHolder: () => handleChangeHolder,
  holderId: () => holderId,
  initDate: () => initDate,
  isAllNormalProduct: () => isAllNormalProduct,
  isAllNormalProductByState: () => isAllNormalProductByState,
  isAppointmentProduct: () => isAppointmentProduct,
  isBookingProduct: () => isBookingProduct,
  isDayBooking: () => isDayBooking,
  isDayBookingProduct: () => isDayBookingProduct,
  isDaysBooking: () => isDaysBooking,
  isEventBooking: () => isEventBooking,
  isHasDayAppointmentProduct: () => isHasDayAppointmentProduct,
  isNormalProductByDurationSchedule: () => isNormalProductByDurationSchedule,
  isOpenDeletePet: () => isOpenDeletePet,
  isTerminalDayBooking: () => isTerminalDayBooking,
  updateAppointmentServicePrice: () => updateAppointmentServicePrice,
  updateNormalBundlePrice: () => updateNormalBundlePrice,
  updateServicePrice: () => updateServicePrice,
  updateServices: () => updateServices
});
module.exports = __toCommonJS(utils_exports);
var import_mock = require("../../mock");
var import_utils = require("../../utils");
var import_utils2 = require("@pisell/utils");
var import_dayjs = __toESM(require("dayjs"));
var import_status = require("./status");
var import_serve = require("../service/serve");
var import_locales = __toESM(require("../../locales"));
var import_utils3 = require("./Group/utils");
var import_utils4 = require("../utils");
var import_utils5 = require("../service/addService/utils");
var import_utils6 = require("../../info2/service/addService/utils");
var import_utilsByBooking = require("../../info2/utilsByBooking");
var import_amount = require("../../components/footer/amount");
import_utils2.locales.init(import_locales.default, localStorage.getItem("umi_locale") || "en");
var formatActiveKey = (lists, platform, isEdit) => {
  var _a;
  if (!(lists == null ? void 0 : lists.length)) return [];
  if (lists.length === 1 && isEdit) {
    return [];
  }
  if (platform === "shop") {
    return [(_a = lists[lists.length - 1]) == null ? void 0 : _a.key];
  }
  return lists == null ? void 0 : lists.map((cart) => cart == null ? void 0 : cart.key);
};
var getResourceItem = (id, state) => {
  var _a;
  if (id === 0) {
    return (0, import_mock.getNoResource)();
  }
  return (_a = state.service.resources) == null ? void 0 : _a.find((d) => d.form_record_id === id);
};
var errorTypes = {
  product: import_utils2.locales.getText("pisell2.text.status.service-error-1"),
  time: import_utils2.locales.getText("pisell2.text.status.service-error-2"),
  maxLimit: import_utils2.locales.getText("pisell2.text.status.service-error-3"),
  limit: import_utils2.locales.getText("pisell2.text.status.service-error-3")
};
var getErrorMessage = (service, resourceItem, isDay) => {
  const errorList = (0, import_utils.getErrorList)(resourceItem, service, isDay);
  let errorMessage = "";
  if (!service.id) {
    return errorList[0].label;
  }
  if (errorList.length) {
    if (errorList.length > 1) {
      errorMessage = `${errorList.length} ${import_utils2.locales.getText(
        "pisell2.text.warnings"
      )}`;
    } else {
      errorMessage = (resourceItem == null ? void 0 : resourceItem.labelText) + " " + errorTypes[errorList[0].type];
    }
  }
  return errorMessage;
};
var formatHolderLists = (lists) => {
  const isOpenGroup = true;
  const _lists = lists == null ? void 0 : lists.map((item) => {
    return {
      ...item,
      id: (item == null ? void 0 : item.id) || (item == null ? void 0 : item.form_record_id),
      key: (item == null ? void 0 : item.id) || (item == null ? void 0 : item.form_record_id),
      label: import_utils2.locales.getText("pisell2.text.cart.of")(item == null ? void 0 : item.main_field)
    };
  });
  if (isOpenGroup) {
    const defaults = [
      {
        key: 0,
        id: 0,
        label: import_utils2.locales.getText("pisell2.text.open.items"),
        main_field: import_utils2.locales.getText("pisell2.text.open.items")
      }
    ];
    return defaults.concat(_lists);
  }
  return _lists;
};
var getHolderLists = (state) => {
  var _a, _b, _c, _d, _e, _f;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  const petLists = ((_f = (_e = state == null ? void 0 : state.pet) == null ? void 0 : _e.value) == null ? void 0 : _f[form_id]) || [];
  const _lists = formatHolderLists(petLists);
  return _lists;
};
var getHolderName = (state, id) => {
  const holderLists = getHolderLists(state);
  if (!holderLists.length || !id) return "";
  const resource = holderLists.find((d) => (d == null ? void 0 : d.id) === id);
  return resource == null ? void 0 : resource.main_field;
};
var isBookingProduct = (extension_type, bol) => {
  return import_status.appointmentTypes.includes(extension_type) || bol;
};
var isDayBookingProduct = (item, state) => {
  return isBookingProduct(item == null ? void 0 : item.extension_type) && state.renderType === "dayBooking";
};
var currentResource = (state, useResource, cacheItem) => {
  const resourcesOrigin = state.service.resourcesOrigin;
  const resources = (0, import_utils5.formatResources)({
    cacheItem,
    resourcesOrigin,
    isDay: false
  });
  let item = resources.find((d) => d.form_record_id == useResource) || {};
  return item;
};
var handleChangeHolder = (products, currentItem, other) => {
  return products.map((item) => {
    if (item._id === currentItem._id) {
      return {
        ...item,
        _extend: {
          ...item._extend,
          holder_id: (0, import_utils2.isArr)(other) ? other.map((item2) => item2.id) : item._extend.quantity > 1 ? [other.id] : other.id
        }
      };
    }
    return item;
  });
};
var handleLike = (products, currentItem, other) => {
  return products.map((item) => {
    var _a, _b;
    if (currentItem.isParallelResource) {
      if (item._id === currentItem.serviceId) {
        return {
          ...item,
          _extend: {
            ...item._extend,
            resource: {
              ...(_a = item._extend) == null ? void 0 : _a.resource,
              [currentItem.resourceId]: (_b = item._extend) == null ? void 0 : _b.resource[currentItem.resourceId].map((item2) => ({
                ...item2,
                like_status: other.like_status
              }))
            }
          }
        };
      }
      return item;
    } else {
      if (item._id === currentItem._id) {
        return {
          ...item,
          _extend: {
            ...item._extend,
            like_status: other.like_status
          }
        };
      }
      return item;
    }
  });
};
var handleChangeResource = (products, currentItem, other, state) => {
  return products.map((item) => {
    var _a;
    const { props, value } = other || {};
    const { isParallelResource, resourceId } = props || {};
    if (isParallelResource) {
      if (item._id === currentItem._id) {
        const hasDataItem = (0, import_utils6.getProductExtend)({
          cacheItem: { ...item, new: 0 },
          state
        });
        const maps = hasDataItem._extend.resource[resourceId] ? hasDataItem._extend.resource[resourceId].reduce((pre, cur) => {
          pre[cur.relation_id] = cur;
          return pre;
        }, {}) : {};
        item._extend.resource[resourceId] = value.map((d) => {
          var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
          const currentForm = ((_a2 = hasDataItem._data.resourceMaps) == null ? void 0 : _a2[resourceId]) || {};
          return (
            // maps[d] 为真时, 说明是之前已选择, 否则为新增的值,补充数据
            maps[d] || {
              relation_type: ((_c = (_b = currentForm.renderListMaps[d]) == null ? void 0 : _b.usable) == null ? void 0 : _c.relation_type) || "form",
              form_id: resourceId,
              relation_id: d,
              like_status: "common",
              // 新添加的数据补充错误信息
              errors: (0, import_utilsByBooking.getErrorList)(
                (_d = currentForm.renderListMaps) == null ? void 0 : _d[d],
                hasDataItem
              ),
              children: (_f = (_e = currentForm.renderListMaps) == null ? void 0 : _e[d]) == null ? void 0 : _f.children,
              metadata: {
                ...(_h = (_g = currentForm.renderListMaps) == null ? void 0 : _g[d]) == null ? void 0 : _h.metadata,
                form_name: currentForm.title,
                resource_name: (_i = currentForm.renderListMaps[d]) == null ? void 0 : _i.main_field
              }
            }
          );
        });
      }
      return item;
    } else {
      if (item._id === currentItem._id) {
        const resourceItem = currentResource(state, other.id, item);
        const { relation_type, item_id, schedule_id } = ((_a = resourceItem == null ? void 0 : resourceItem._extend) == null ? void 0 : _a.usableTime) || {};
        return {
          ...item,
          _extend: {
            ...item._extend,
            resource: item_id || (resourceItem == null ? void 0 : resourceItem.form_record_id) || 0,
            relation_type: relation_type || "form",
            relation_id: item_id || (resourceItem == null ? void 0 : resourceItem.form_record_id) || 0,
            schedule_id: schedule_id || 0
          }
        };
      }
      return item;
    }
  });
};
var updateServices = (state, currentItem, type, other, actions) => {
  var _a, _b, _c;
  if (!((_a = state == null ? void 0 : state.service) == null ? void 0 : _a.value)) return [];
  let products = ((_b = state == null ? void 0 : state.service) == null ? void 0 : _b.value) || [];
  const isGroup = (0, import_utils3.isGroupBooking)(currentItem._id);
  if (type === "delete") {
    let deleteIds = [];
    if (isGroup) {
      deleteIds = (0, import_utils3.getProductByGroupId)(currentItem._id, products);
    } else {
      deleteIds = [currentItem._id];
    }
    products = products == null ? void 0 : products.filter((item) => !deleteIds.includes(item == null ? void 0 : item._id));
  }
  if (type === "delete_holders") {
    products = products == null ? void 0 : products.filter(
      (item) => {
        var _a2;
        return ((_a2 = item == null ? void 0 : item._extend) == null ? void 0 : _a2.holder_id) !== other;
      }
    );
  }
  if (type === "change_holder") {
    products = handleChangeHolder(products, currentItem, other);
    return products;
  }
  if (type === "add_holder") {
    (_c = actions == null ? void 0 : actions.handleAddHolder) == null ? void 0 : _c.call(actions, products, currentItem);
    return products;
  }
  if (type === "like") {
    products = handleLike(products, currentItem, other);
  }
  if (type === "change_resource") {
    products = handleChangeResource(products, currentItem, other, state);
    return products;
  }
  if (type === "update_holders") {
    products = products == null ? void 0 : products.map((item) => {
      var _a2;
      const ids = currentItem.map((d) => d._id);
      if (ids == null ? void 0 : ids.includes(item == null ? void 0 : item._id)) {
        let _extend = item._extend || {};
        if (Array.isArray(other)) {
          _extend.holder_id = (_a2 = other.find(
            (other2) => (other2 == null ? void 0 : other2._id) === (item == null ? void 0 : item._id)
          )) == null ? void 0 : _a2.holder_id;
        } else {
          _extend.holder_id = other;
        }
        return item;
      }
      return item;
    });
  } else {
    products = products == null ? void 0 : products.map((item) => {
      let updateIds = [];
      if (isGroup) {
        updateIds = (0, import_utils3.getProductByGroupId)(currentItem._id, products);
      } else {
        updateIds = [currentItem._id];
      }
      if (updateIds.includes(item._id)) {
        if (type === "like") {
          let _extend = item._extend || {};
          _extend.like_status = other == null ? void 0 : other.like_status;
        } else {
          item = {
            ...item,
            ...other
          };
        }
        return item;
      }
      return item;
    });
  }
  return products;
};
var formatBundle = (bundle) => {
  if (!(bundle == null ? void 0 : bundle.length)) return [];
  return bundle.map((item) => {
    return {
      id: item == null ? void 0 : item.id,
      title: item == null ? void 0 : item.title,
      num: item == null ? void 0 : item.num,
      price: item == null ? void 0 : item.price,
      total: item == null ? void 0 : item.price,
      price_type: item == null ? void 0 : item.price_type,
      bundle_sum_price: item == null ? void 0 : item.bundle_sum_price,
      bundle_sum_type: item == null ? void 0 : item.bundle_sum_type,
      options: formatOptions(item == null ? void 0 : item.option)
    };
  });
};
var formatOptions = (options) => {
  if (!(options == null ? void 0 : options.length)) return [];
  return options.map((item) => {
    const price = Number((item == null ? void 0 : item.price) || (item == null ? void 0 : item.add_price));
    return {
      id: item == null ? void 0 : item.id,
      name: item == null ? void 0 : item.name,
      num: item == null ? void 0 : item.num,
      price,
      total: price,
      origin_total: item == null ? void 0 : item.original_price
    };
  });
};
var formItems = (items) => {
  return items.map((d) => {
    return {
      ...d,
      start_date: d._extend.start_date,
      end_date: d._extend.end_date,
      num: d == null ? void 0 : d._extend.quantity,
      total: d._extend.total,
      origin_total: d._extend.origin_total
    };
  });
};
var formatHolders = (items) => {
  return items.map((d) => {
    return {
      id: d == null ? void 0 : d.form_record_id,
      label: d == null ? void 0 : d.main_field
    };
  });
};
var getErrorMessageInfo2 = (errorList) => {
  let errorMessage = "";
  if (errorList == null ? void 0 : errorList.length) {
    if (errorList.length === 1) {
      errorMessage = errorList[0].label + " " + errorTypes[errorList[0].type];
    } else {
      errorMessage = `${errorList.length} ${import_utils2.locales.getText(
        "pisell2.text.warnings"
      )}`;
    }
  }
  return errorMessage;
};
var getAllErrorList = (item) => {
  const { resourceMaps } = item._data || {};
  const { resource = {} } = item._extend || {};
  const errorList = [];
  Object.entries(resource).forEach(([key, value]) => {
    value.forEach((val) => {
      var _a;
      const list = (0, import_utilsByBooking.getErrorList)((_a = resourceMaps[key]) == null ? void 0 : _a.renderListMaps[val.relation_id], item);
      errorList.push(...list);
      console.log(list, "list");
    });
  });
  return errorList;
};
var mapListItems = (items) => items.map((item) => ({
  id: item.id || item.form_record_id,
  label: item.labelText,
  isError: !item.usable || item.unbound
}));
var getIsSubjectRequire = (state) => {
  var _a, _b, _c, _d, _e, _f, _g;
  return ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.type) === "form" && ((_g = (_f = (_e = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _e.config) == null ? void 0 : _f.basic) == null ? void 0 : _g.subject_require) === "require";
};
var isNormalProductByDurationSchedule = (item) => {
  var _a, _b;
  return !(item == null ? void 0 : item.duration) && !((_a = item == null ? void 0 : item.schedules) == null ? void 0 : _a.length) && !((_b = item["schedule.ids"]) == null ? void 0 : _b.length);
};
var isAllNormalProduct = (items) => {
  return items.every((item) => isNormalProductByDurationSchedule(item));
};
var isAllNormalProductByState = (state) => {
  return isAllNormalProduct(state.service.value);
};
var getGeneralParams = (item, state, isParallelResource) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const { pet, service } = state;
  const { holders = [], formData } = pet || {};
  const parallelResourceOtherParams = {};
  let multiResources;
  if (isParallelResource) {
    const hasDataItem = (0, import_utils6.getProductExtend)({
      cacheItem: { ...item, new: 0 },
      state
    });
    const { resourceMaps, resources } = hasDataItem._data || {};
    parallelResourceOtherParams.errorMessage = getErrorMessageInfo2(getAllErrorList(hasDataItem));
    multiResources = resources.map((resource) => {
      var _a2, _b2;
      const key = resource.form_id;
      const detail = resourceMaps == null ? void 0 : resourceMaps[key];
      const isMultiple = ((_a2 = detail == null ? void 0 : detail.select_type) == null ? void 0 : _a2.type) === "multiple";
      const value = item._extend.resource[key];
      return {
        is_hide_like: isMultiple,
        isParallelResource: true,
        maxLength: isMultiple && detail.select_type.max,
        resourceMode: isMultiple ? "multiple" : void 0,
        serviceId: item._id,
        resourceId: key,
        _id: key,
        id: (value || []).map((val) => val.relation_id),
        status: ((_b2 = value == null ? void 0 : value[0]) == null ? void 0 : _b2.like_status) || "common",
        lists: (detail == null ? void 0 : detail.renderList) ? mapListItems(detail == null ? void 0 : detail.renderList) : [],
        disabled: state.disabledEdit || state.disableEventBookingEdit
      };
    });
  } else {
    const { resourcesOrigin } = service || {};
    const resource = (0, import_utils5.formatResources)({
      cacheItem: item,
      resourcesOrigin,
      isDay: false
    });
    multiResources = [
      {
        _id: item._id,
        id: item._extend.resource,
        status: ((_a = item._extend) == null ? void 0 : _a.like_status) || "common",
        lists: mapListItems(resource)
      }
    ];
  }
  const isNormalProduct = isNormalProductByDurationSchedule(item);
  return {
    format: ["HH:mm", "HH:mm"],
    show_time: true,
    duration_type: "minutes",
    // 当前可选择holder列表
    holders: formatHolders(holders),
    isShowChangeHolder: !isNormalProduct,
    isShowAction: (0, import_utils4.isShowAddTime)(state, item),
    actionText: import_utils2.locales.getText("page.booking.text.add-time"),
    isMultipleResource: true,
    multiResources,
    holderMaxCount: item == null ? void 0 : item._extend.quantity,
    holderMode: (item == null ? void 0 : item._extend.quantity) > 1 ? "multiple" : void 0,
    isErrorHolder: getIsSubjectRequire(state),
    addHolderButtonText: ((_c = (_b = formData == null ? void 0 : formData[0]) == null ? void 0 : _b.form) == null ? void 0 : _c.title) || "",
    holder_id: (_d = item == null ? void 0 : item._extend) == null ? void 0 : _d.holder_id,
    start_date: !isNormalProduct ? (_e = item == null ? void 0 : item._extend) == null ? void 0 : _e.startDate : "",
    end_date: !isNormalProduct ? ((_f = item == null ? void 0 : item._extend) == null ? void 0 : _f.endDate) || ((_g = item == null ? void 0 : item._extend) == null ? void 0 : _g.end_date) : "",
    rowKey: `rowKey-${(_i = (_h = item == null ? void 0 : item._extend) == null ? void 0 : _h.other) == null ? void 0 : _i.rowKey}`,
    ...parallelResourceOtherParams
  };
};
var calcAddTimeTotalPrice = (total, addTimeProducts) => {
  return (Number(total) || 0) + (0, import_amount.getRelationDetailsTotal)(addTimeProducts || []);
};
var formatProducts = (lists, state, isRenderGroup = true, isGeneral, isParallelResource) => {
  if (!(lists == null ? void 0 : lists.length)) return [];
  const normalList = [];
  const groupList = [];
  const formItem = (item) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
    const _isDayBookingProduct = isDayBookingProduct(item, state);
    const clientTitle = getHolderName(state, (_a = item == null ? void 0 : item._extend) == null ? void 0 : _a.holder_id);
    const options = formatOptions((_c = (_b = item == null ? void 0 : item._extend) == null ? void 0 : _b.other) == null ? void 0 : _c.option);
    const bundle = formatBundle((_e = (_d = item == null ? void 0 : item._extend) == null ? void 0 : _d.other) == null ? void 0 : _e.bundle);
    const isNormalProduct = !import_status.appointmentTypes.includes(item == null ? void 0 : item.extension_type) || (state == null ? void 0 : state.renderType) === "eventBooking";
    let resourceItem = {};
    let errorMessage = "";
    if (_isDayBookingProduct || isGeneral) {
      resourceItem = getResourceItem((_f = item == null ? void 0 : item._extend) == null ? void 0 : _f.resource, state);
      errorMessage = getErrorMessage(item, resourceItem, isDayBooking(state));
    }
    const generalParams = isGeneral ? getGeneralParams(item, state, !!isParallelResource) : {};
    return {
      id: item == null ? void 0 : item.id,
      _id: item == null ? void 0 : item._id,
      name: item == null ? void 0 : item.title,
      price: (_g = item == null ? void 0 : item._extend) == null ? void 0 : _g.price,
      total: calcAddTimeTotalPrice((_h = item == null ? void 0 : item._extend) == null ? void 0 : _h.total, (_i = item == null ? void 0 : item._extend) == null ? void 0 : _i.relation_details),
      origin_total: calcAddTimeTotalPrice((_j = item == null ? void 0 : item._extend) == null ? void 0 : _j.origin_total, (_k = item == null ? void 0 : item._extend) == null ? void 0 : _k.relation_details),
      num: item == null ? void 0 : item._extend.quantity,
      image: item == null ? void 0 : item.cover,
      isShowNote: true,
      note: item == null ? void 0 : item.note,
      like_status: (_l = item == null ? void 0 : item._extend) == null ? void 0 : _l.like_status,
      bundle,
      options,
      errorMessage,
      product_option_string: (_n = (_m = item == null ? void 0 : item._extend) == null ? void 0 : _m.other) == null ? void 0 : _n.product_option_string,
      discount_reason: (_o = item == null ? void 0 : item._extend) == null ? void 0 : _o.discount_reason,
      resource_id: isNormalProduct ? 0 : (_p = item == null ? void 0 : item._extend) == null ? void 0 : _p.resource,
      relation_form_name: isNormalProduct ? "" : (resourceItem == null ? void 0 : resourceItem.labelText) || item.relation_form_name,
      holder_title: clientTitle,
      start_date: !isNormalProduct ? (_q = item == null ? void 0 : item._extend) == null ? void 0 : _q.start_date : "",
      end_date: !isNormalProduct ? (_r = item == null ? void 0 : item._extend) == null ? void 0 : _r.end_date : "",
      holder_id: ((_s = item == null ? void 0 : item._extend) == null ? void 0 : _s.holder_id) || 0,
      isNormalProduct,
      locale: state.locale,
      symbol: state.amountSymbol,
      relation_products: ((_t = item == null ? void 0 : item._extend) == null ? void 0 : _t.relation_details) || [],
      items: isEventBooking(state) ? [] : formItems(((_u = item == null ? void 0 : item._extend) == null ? void 0 : _u.items) || []),
      ...generalParams,
      _origin: item
    };
  };
  console.log(normalList, "normalList");
  if (isRenderGroup) {
    lists.forEach((item) => {
      var _a;
      if (!((_a = item == null ? void 0 : item._extend) == null ? void 0 : _a.groupId)) {
        normalList.push(formItem(item));
      } else {
        groupList.push(item);
      }
    });
    let groups = groupList.reduce((prev, item) => {
      var _a;
      const groupId = (_a = item == null ? void 0 : item._extend) == null ? void 0 : _a.groupId;
      if (!prev[groupId]) {
        prev[groupId] = [];
      }
      prev[groupId].push(item);
      return prev;
    }, {});
    return Object.values(groups).map((d) => formItem((0, import_utils3.concatBookings)(d))).concat(normalList);
  } else {
    return lists == null ? void 0 : lists.map((item) => formItem(item));
  }
};
var disabledDate = (current, position, currentDate) => {
  if (!(0, import_dayjs.default)(currentDate).isValid()) return;
  if (position === "start") {
    return false;
  }
  if (position === "end" && current.endOf("day") && currentDate) {
    const endDisabled = currentDate.add(8, "months").format("YYYY-MM-DD");
    return (0, import_dayjs.default)(endDisabled) < current.startOf("day") || current.startOf("day") <= currentDate;
  }
  return current && current <= currentDate;
};
var isDayBooking = (state) => {
  return (state == null ? void 0 : state.renderType) === "dayBooking" || (state == null ? void 0 : state.renderType) === "eventBooking";
};
var isDaysBooking = (state) => {
  return (state == null ? void 0 : state.renderType) === "dayBooking";
};
var isEventBooking = (state) => {
  return state.renderType === "eventBooking";
};
var isAppointmentProduct = (item) => {
  return import_status.appointmentTypes.includes(item.extension_type);
};
var isTerminalDayBooking = (state) => {
  return state.isTerminalDayBookingPage;
};
var formatDayDate = (_extend) => {
  let startDate = (0, import_dayjs.default)(_extend == null ? void 0 : _extend.start_date).startOf("day");
  let endDate = (0, import_dayjs.default)(_extend == null ? void 0 : _extend.end_date).startOf("day");
  return {
    ..._extend,
    start_date: startDate,
    end_date: endDate
  };
};
var formatDayTime = ({ startDate, endDate }) => {
  const shopOpeningTime = localStorage.getItem("shopOpeningHours") || "00:00";
  let start_date = (0, import_dayjs.default)(startDate).format("YYYY-MM-DD");
  let end_date = (0, import_dayjs.default)(endDate).format("YYYY-MM-DD");
  const result = {
    startDateTime: (0, import_dayjs.default)(`${start_date}T${shopOpeningTime}:00`),
    // 结束日期根据营业日时间减去一分钟
    endDateTime: (0, import_dayjs.default)(`${end_date}T${shopOpeningTime}:00`).subtract(
      1,
      "minute"
    )
  };
  return result;
};
var formatDateTime = (start_date, end_date) => {
  const _openingTime = (0, import_status.openingTime)();
  const startDate = `${start_date} ${_openingTime}`;
  let endDate = `${end_date} ${_openingTime}`;
  const date = {
    value: (0, import_dayjs.default)(startDate),
    end_date: (0, import_dayjs.default)(endDate).subtract(1, "minute")
  };
  return date;
};
var initDate = () => {
  const startDate = (0, import_dayjs.default)().format("YYYY-MM-DD");
  let endDate = (0, import_dayjs.default)().add(1, "days").format("YYYY-MM-DD");
  const date = formatDateTime(startDate, endDate);
  return date;
};
var isHasDayAppointmentProduct = (state) => {
  var _a, _b, _c, _d, _e, _f, _g;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  const services = ((_e = state.service) == null ? void 0 : _e.value) || [];
  const pets = ((_g = (_f = state.pet) == null ? void 0 : _f.value) == null ? void 0 : _g[form_id]) || [];
  if (!pets.length) return true;
  const checkType = (extension_type) => {
    if (isEventBooking(state)) {
      return true;
    }
    return import_status.appointmentTypes.includes(extension_type);
  };
  const appointmentServices = services.filter(
    (d) => checkType(d.extension_type)
  );
  if (!appointmentServices.length) return true;
  let _pets = pets == null ? void 0 : pets.map((d) => {
    return {
      ...d,
      // 如果该购物车中没选择任何商品，则不需要验证，所以直接用1来填充
      // services: !holderProducts?.length ? [1] : holderProducts?.filter((d: any) => appointmentTypes.includes(d.extension_type))
      services: services.filter(
        (item) => {
          var _a2;
          return ((_a2 = item._extend) == null ? void 0 : _a2.holder_id) == d.form_record_id && checkType(item.extension_type);
        }
      )
    };
  });
  _pets = _pets.filter((d) => !d.services.length);
  return _pets.length > 0;
};
var getDayProducts = (holderId2, lists, isTerminal) => {
  let _lists = lists || [];
  if (!holderId2 && !isTerminal) {
    _lists = _lists.filter(
      (item) => !import_status.appointmentTypes.includes(item == null ? void 0 : item.extension_type)
    );
  } else {
    _lists = _lists.filter(
      (item) => {
        var _a;
        return !import_status.appointmentTypes.includes(item == null ? void 0 : item.extension_type) || import_status.appointmentTypes.includes(item == null ? void 0 : item.extension_type) && ((_a = item.duration) == null ? void 0 : _a.type) === "days";
      }
    );
  }
  return _lists;
};
var getPets = (state) => {
  var _a, _b, _c, _d, _e, _f;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  const pets = ((_f = (_e = state == null ? void 0 : state.pet) == null ? void 0 : _e.value) == null ? void 0 : _f[form_id]) || [];
  return pets;
};
var getAppointmentService = (state) => {
  var _a;
  const services = ((_a = state.service) == null ? void 0 : _a.value) || [];
  let lists = services.filter(
    (item) => import_status.appointmentTypes.includes(item.extension_type)
  );
  return lists;
};
var isOpenDeletePet = (state, newVal) => {
  var _a, _b, _c, _d, _e, _f;
  const form_id = ((_d = (_c = (_b = (_a = state == null ? void 0 : state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.appointment_subject) == null ? void 0 : _d.form_id) || 0;
  const oldPets = ((_f = (_e = state == null ? void 0 : state.pet) == null ? void 0 : _e.value) == null ? void 0 : _f[form_id]) || [];
  const newPets = (newVal == null ? void 0 : newVal[form_id]) || [];
  const deletePets = oldPets.filter(
    (item) => !newPets.some(
      (newItem) => newItem.form_record_id === item.form_record_id
    )
  );
  const servicePets = deletePets.map((item) => {
    var _a2, _b2;
    return {
      ...item,
      services: (_b2 = (_a2 = state.service) == null ? void 0 : _a2.value) == null ? void 0 : _b2.filter(
        (d) => {
          var _a3;
          return ((_a3 = d._extend) == null ? void 0 : _a3.holder_id) == item.form_record_id;
        }
      )
    };
  });
  const lists = servicePets.filter((item) => {
    var _a2;
    return ((_a2 = item.services) == null ? void 0 : _a2.length) > 0;
  });
  return {
    ids: lists.map((item) => item.form_record_id),
    lists
  };
};
var holderId = (state) => {
  var _a;
  return Number(((_a = state == null ? void 0 : state.activeCartId) == null ? void 0 : _a[0]) || 0);
};
function calculateTotalPrice(data) {
  const _price = data == null ? void 0 : data.reduce(
    (total, item) => total + ((item == null ? void 0 : item.price) || (item == null ? void 0 : item.add_price)) * item.num,
    0
  );
  return parseFloat(_price);
}
function updateSinglePrice(currentValue, single) {
  const _single = [];
  for (let index = 0; index < currentValue.length; index++) {
    const item = currentValue[index];
    const currentIndex = single.findIndex(
      (_single2) => _single2.id === item.id
    );
    if (currentIndex !== -1) {
      item.add_price = single[currentIndex].add_price;
      item.name = single[currentIndex].name;
      _single.push(item);
    }
  }
  return _single;
}
function updateBundlePrice(currentValue, [bundle, single]) {
  var _a;
  const _bundle = [];
  for (let index = 0; index < currentValue.length; index++) {
    const item = currentValue[index];
    const currentIndex = bundle.findIndex(
      (_bundle2) => _bundle2.id === item.id
    );
    if (currentIndex !== -1 && bundle[currentIndex]) {
      item.title = bundle[currentIndex].title;
      item.price_type = bundle[currentIndex].price_type;
      item._price = bundle[currentIndex].price;
      const bundle_sum_price = parseFloat(
        bundle[currentIndex].price_type === "markdown" ? bundle[currentIndex].price * -1 : bundle[currentIndex].price
      ).toFixed(2);
      item.price = bundle_sum_price;
      item.bundle_sum_price = bundle_sum_price;
      if ((_a = item == null ? void 0 : item.option) == null ? void 0 : _a.length) {
        const _option = JSON.parse(JSON.stringify(item.option));
        item.option = updateSinglePrice(_option, single);
        let optionPrice = 0;
        optionPrice = calculateTotalPrice(item.option);
        const bundle_sum_price2 = (parseFloat(
          bundle[currentIndex].price_type === "markdown" ? bundle[currentIndex].price * -1 : bundle[currentIndex].price
        ) + optionPrice).toFixed(2);
        item.price = bundle_sum_price2;
        item.bundle_sum_price = bundle_sum_price2;
      }
      _bundle.push(item);
    }
  }
  return _bundle;
}
function flatData(newPackageData) {
  var _a, _b, _c;
  let bundle = [];
  let single = [];
  for (const group of newPackageData) {
    if ((_a = group == null ? void 0 : group.bundle_item) == null ? void 0 : _a.length) {
      for (const item of group.bundle_item) {
        bundle.push(item);
      }
    }
  }
  for (const bundleItem of bundle) {
    if ((_b = bundleItem == null ? void 0 : bundleItem.option_group) == null ? void 0 : _b.length) {
      for (const group of bundleItem.option_group) {
        if ((_c = group == null ? void 0 : group.option_item) == null ? void 0 : _c.length) {
          for (const item of group.option_item) {
            single.push(item);
          }
        }
      }
    }
  }
  return [bundle, single];
}
function updateNormalBundlePrice(currentValue, newBundleData) {
  const _currentValue = JSON.parse(JSON.stringify(currentValue));
  const _flatData = flatData(newBundleData);
  const newValue = updateBundlePrice(_currentValue, _flatData);
  return newValue;
}
var normalServicePrice = async (ids, state) => {
  var _a, _b, _c, _d;
  const services = ((_a = state.service) == null ? void 0 : _a.value) || [];
  const customerId = ((_c = (_b = state == null ? void 0 : state.client) == null ? void 0 : _b.value) == null ? void 0 : _c.id) || 0;
  let data = await state.apis.getServices({
    skip: 1,
    num: 500,
    ids,
    customer_id: customerId,
    open_quotation: 1,
    open_bundle: 1,
    with: ["bundleGroup.bundleItem", "optionGroup.optionItem"],
    _config: { abort: true }
  });
  if (!((_d = data == null ? void 0 : data.list) == null ? void 0 : _d.length)) return;
  const _lists = services.map((item) => {
    var _a2, _b2, _c2, _d2;
    let obj = { ...item };
    if (!import_status.appointmentTypes.includes(obj.extension_type)) {
      const product = data.list.find((p) => p.id === (item == null ? void 0 : item.product_id));
      let _extend = (obj == null ? void 0 : obj._extend) || {};
      const other = (_extend == null ? void 0 : _extend.other) || {};
      if (product) {
        if (((_a2 = product == null ? void 0 : product.variant) == null ? void 0 : _a2.length) && (other == null ? void 0 : other.product_variant_id)) {
          const _variant = (_b2 = product == null ? void 0 : product.variant) == null ? void 0 : _b2.find(
            (v) => v.id === (other == null ? void 0 : other.product_variant_id)
          );
          obj._extend.price = Number(_variant == null ? void 0 : _variant.price);
          obj._extend.other.price = Number(_variant == null ? void 0 : _variant.price);
        } else {
          obj._extend.price = Number(product == null ? void 0 : product.price);
        }
        if (((_c2 = other == null ? void 0 : other.bundle) == null ? void 0 : _c2.length) && ((_d2 = product == null ? void 0 : product.bundle_group) == null ? void 0 : _d2.length)) {
          let _bundle = updateNormalBundlePrice(
            other.bundle,
            product == null ? void 0 : product.bundle_group
          );
          obj._extend.other = {
            ...other,
            bundle: _bundle
          };
        }
        obj._extend.total = (0, import_utils.getProductTotalPrice)(obj);
        obj._extend.origin_total = (0, import_utils.getProductTotalPrice)(obj);
      }
    }
    return obj;
  });
  return _lists;
};
var updateAppointmentServicePrice = async (state, { start_date, end_date, product_id }) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const defaultReturn = {
    multiDayPrices: [],
    price: Number((_c = (_b = (_a = state == null ? void 0 : state.service) == null ? void 0 : _a.cacheItem) == null ? void 0 : _b._extend) == null ? void 0 : _c.price) || 0
  };
  try {
    const format = "YYYY-MM-DD";
    const customer_id = ((_e = (_d = state == null ? void 0 : state.client) == null ? void 0 : _d.value) == null ? void 0 : _e.id) || 1;
    const _product_id = product_id || ((_f = state.service.cacheItem) == null ? void 0 : _f.product_id) || 0;
    let multi_day_price = null;
    if (!product_id) {
      console.log("updateAppointmentServicePrice", product_id);
      return defaultReturn;
    }
    const startDate = (0, import_dayjs.default)(start_date).format(format);
    const endDate = (0, import_dayjs.default)(end_date).subtract(1, "days").format(format);
    const params = {
      customer_id,
      multi_day: { start: startDate, end: endDate }
    };
    const res = await (0, import_serve.postMultiDay)(_product_id, params);
    const _prices = ((_g = res == null ? void 0 : res.data) == null ? void 0 : _g.multi_day_price) || [];
    multi_day_price = _prices == null ? void 0 : _prices.map((p) => {
      var _a2, _b2;
      const startDate2 = (0, import_dayjs.default)((_a2 = p == null ? void 0 : p.multi_day) == null ? void 0 : _a2.start);
      const endDate2 = (0, import_dayjs.default)((_b2 = p == null ? void 0 : p.multi_day) == null ? void 0 : _b2.end).add(1, "day");
      let duration = (endDate2 == null ? void 0 : endDate2.diff(startDate2, "day")) || 1;
      return {
        total: Number(p == null ? void 0 : p.price),
        origin_total: Number(p == null ? void 0 : p.price),
        num: duration,
        quotation_id: (p == null ? void 0 : p.quotation_id) || 0,
        start_date: startDate2 == null ? void 0 : startDate2.format(format),
        end_date: endDate2 == null ? void 0 : endDate2.format(format)
      };
    });
    return {
      multiDayPrices: multi_day_price,
      price: Number((_h = res == null ? void 0 : res.data) == null ? void 0 : _h.price) || 0
    };
  } catch (error) {
    console.log("updateAppointmentServicePrice", error);
  }
  return defaultReturn;
};
var updateServicePrice = (state) => {
  var _a;
  let services = ((_a = state.service) == null ? void 0 : _a.value) || [];
  let normalServices = services == null ? void 0 : services.filter(
    (item) => isEventBooking(state) ? true : !import_status.appointmentTypes.includes(item.extension_type)
  );
  let newNormalIds = normalServices == null ? void 0 : normalServices.map(
    (item) => (item == null ? void 0 : item.product_id) || (item == null ? void 0 : item.id)
  );
  newNormalIds = [...new Set(newNormalIds)];
  if (newNormalIds == null ? void 0 : newNormalIds.length) {
    return normalServicePrice(newNormalIds, state);
  }
  return [];
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  disabledDate,
  formItems,
  formatActiveKey,
  formatDateTime,
  formatDayDate,
  formatDayTime,
  formatHolderLists,
  formatProducts,
  getAppointmentService,
  getDayProducts,
  getErrorMessage,
  getIsSubjectRequire,
  getPets,
  getResourceItem,
  handleChangeHolder,
  holderId,
  initDate,
  isAllNormalProduct,
  isAllNormalProductByState,
  isAppointmentProduct,
  isBookingProduct,
  isDayBooking,
  isDayBookingProduct,
  isDaysBooking,
  isEventBooking,
  isHasDayAppointmentProduct,
  isNormalProductByDurationSchedule,
  isOpenDeletePet,
  isTerminalDayBooking,
  updateAppointmentServicePrice,
  updateNormalBundlePrice,
  updateServicePrice,
  updateServices
});
