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 { getUniqueId, locales } from "@pisell/utils";
import { PisellToast } from '@pisell/materials';
/**
 * @title: 格式化holder表数据
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-07 21:22
 */
export var formatHolder = function formatHolder(data) {
  return data === null || data === void 0 ? void 0 : data.map(function (d) {
    return _objectSpread(_objectSpread({}, d), {}, {
      key: d === null || d === void 0 ? void 0 : d.id,
      label: d === null || d === void 0 ? void 0 : d.username,
      name: d === null || d === void 0 ? void 0 : d.username
    });
  });
};
export var formatGuest = function formatGuest(data, products) {
  return data === null || data === void 0 ? void 0 : data.map(function (d) {
    var children = products.filter(function (p) {
      return (p === null || p === void 0 ? void 0 : p.holder_id) === (d === null || d === void 0 ? void 0 : d.id);
    }).map(function (p) {
      return {
        name: p === null || p === void 0 ? void 0 : p.name
      };
    });
    return _objectSpread(_objectSpread({}, d), {}, {
      key: d === null || d === void 0 ? void 0 : d.id,
      name: d === null || d === void 0 ? void 0 : d.username,
      children: children || []
    });
  });
};

/**
 * @title: 格式化购物车中资源上挂接的表单数据
 * @description: 
 * @param {any} data
 * @param {any} resources
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-06 22:22
 */
export var formatCartsResources = function formatCartsResources(carts, resources, setPolicyIds) {
  try {
    var policyIds = [];
    var newCarts = carts === null || carts === void 0 ? void 0 : carts.map(function (d) {
      var _currentResource$reso;
      var currentResource = resources === null || resources === void 0 ? void 0 : resources.find(function (r) {
        return (d === null || d === void 0 ? void 0 : d._id) === (r === null || r === void 0 ? void 0 : r._id);
      });
      var currentResources = currentResource === null || currentResource === void 0 || (_currentResource$reso = currentResource.resources) === null || _currentResource$reso === void 0 || (_currentResource$reso = _currentResource$reso[0]) === null || _currentResource$reso === void 0 ? void 0 : _currentResource$reso.renderList;

      // 当前购物车选择的资源id
      var resourceId = d === null || d === void 0 ? void 0 : d.resource_id;

      // 从资源列表中找到对应的资源数据
      var foundResource = currentResources === null || currentResources === void 0 ? void 0 : currentResources.find(function (resource) {
        return resource.id === resourceId;
      });
      if (foundResource !== null && foundResource !== void 0 && foundResource.resource_form_id) {
        return _objectSpread(_objectSpread({}, d), {}, {
          resource_form_id: foundResource === null || foundResource === void 0 ? void 0 : foundResource.resource_form_id
        });
      } else if (foundResource !== null && foundResource !== void 0 && foundResource.policy_id && !policyIds.includes(foundResource === null || foundResource === void 0 ? void 0 : foundResource.policy_id)) {
        // 资源上没有挂接表单，但是有协议，则在触发按钮的时候需要同意协议才能继续
        policyIds.push(foundResource === null || foundResource === void 0 ? void 0 : foundResource.policy_id);
      }
      return false;
    }).filter(Boolean);
    setPolicyIds(policyIds);
    return newCarts || [];
  } catch (error) {
    console.log(error, 'formatCarts');
    return [];
  }
};

/**
 * @title: 格式化购物车数据
 * @description: 
 * @param {any} data
 * @param {any} resources
 * @param {any} accountLists
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-07 21:22
 */
export var formatCarts = function formatCarts(data, resources, utils, accountLists) {
  try {
    var _utils$storage;
    if (!utils || !(data !== null && data !== void 0 && data.length)) return [];
    var locale = (utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 ? void 0 : _utils$storage.get('umi_locale')) || 'en-US';
    var symbol = (utils === null || utils === void 0 ? void 0 : utils.getSymbolic()) || '$';
    var newCarts = data === null || data === void 0 ? void 0 : data.map(function (d) {
      var _currentResource$reso2, _currentResources, _d$duration;
      var currentResource = resources === null || resources === void 0 ? void 0 : resources.find(function (r) {
        return (d === null || d === void 0 ? void 0 : d._id) === (r === null || r === void 0 ? void 0 : r._id);
      });
      var currentResources = currentResource === null || currentResource === void 0 || (_currentResource$reso2 = currentResource.resources) === null || _currentResource$reso2 === void 0 || (_currentResource$reso2 = _currentResource$reso2[0]) === null || _currentResource$reso2 === void 0 ? void 0 : _currentResource$reso2.renderList;
      currentResources = (_currentResources = currentResources) === null || _currentResources === void 0 ? void 0 : _currentResources.map(function (d) {
        return _objectSpread(_objectSpread({}, d), {}, {
          label: (d === null || d === void 0 ? void 0 : d.main_field) || (d === null || d === void 0 ? void 0 : d.id)
        });
      });
      return _objectSpread(_objectSpread({}, d), {}, {
        format: ['HH:mm', 'HH:mm'],
        show_time: true,
        duration_type: (d === null || d === void 0 || (_d$duration = d.duration) === null || _d$duration === void 0 ? void 0 : _d$duration.type) || 'minutes',
        locale: locale,
        symbol: symbol,
        resources: currentResources,
        is_hide_like: true,
        holders: formatHolder(accountLists),
        allowClearHolder: true,
        allowClearResource: true
      });
    });
    return newCarts || [];
  } catch (error) {
    console.log(error, 'formatCarts');
    return [];
  }
};

/**
 * @title: 对资源进行排序
 * @description: 
 * @param {any} currentResourcesMap
 * @param {any} resourceSort
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-14 11:16
 */
export var sortResources = function sortResources(resources, resourceSort) {
  try {
    var sortedResources = resources.sort(function (a, b) {
      var aIndex = resourceSort.indexOf(a.code);
      var bIndex = resourceSort.indexOf(b.code);
      return aIndex - bIndex;
    });

    // 格式化资源数据列表
    sortedResources = sortedResources.map(function (resource) {
      var _resource$renderList;
      var renderList = resource === null || resource === void 0 || (_resource$renderList = resource.renderList) === null || _resource$renderList === void 0 ? void 0 : _resource$renderList.map(function (d) {
        var _d$cover;
        return _objectSpread(_objectSpread({}, d), {}, {
          image: d === null || d === void 0 || (_d$cover = d.cover) === null || _d$cover === void 0 || (_d$cover = _d$cover[0]) === null || _d$cover === void 0 ? void 0 : _d$cover.url,
          title: d === null || d === void 0 ? void 0 : d.main_field,
          subtitle: d === null || d === void 0 ? void 0 : d.sub_field
        });
      });
      return _objectSpread(_objectSpread({}, resource), {}, {
        renderList: renderList
      });
    });
    return sortedResources || [];
  } catch (error) {
    console.log(error, 'wh2222');
  }
};
export var getMeAccount = function getMeAccount(val) {
  return val === null || val === void 0 ? void 0 : val.find(function (d) {
    return !(d !== null && d !== void 0 && d.isGuest);
  });
};
export var getCustomer = function getCustomer(utils) {
  try {
    var _utils$getStore$getSt, _userInfo;
    if (!utils) return;
    var userInfo = (_utils$getStore$getSt = utils.getStore().getState()) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.global) === null || _utils$getStore$getSt === void 0 ? void 0 : _utils$getStore$getSt.userInfo;
    var localUserInfo = JSON.parse(utils.storage.get('customer') || '{}');
    userInfo = (_userInfo = userInfo) !== null && _userInfo !== void 0 && _userInfo.id ? userInfo : localUserInfo;
    return userInfo;
  } catch (error) {
    console.log("getCustomer ".concat(error));
  }
};
export var formatCustomer = function formatCustomer(val) {
  var currentUserName = "".concat((val === null || val === void 0 ? void 0 : val.first_name) || '').concat(val !== null && val !== void 0 && val.last_name ? " ".concat(val === null || val === void 0 ? void 0 : val.last_name) : '');
  currentUserName = currentUserName || (val === null || val === void 0 ? void 0 : val.nickname) || 'Me';
  return {
    id: (val === null || val === void 0 ? void 0 : val.id) || getUniqueId(),
    username: currentUserName
  };
};
export var formatCurrentUser = function formatCurrentUser(utils) {
  var userInfo = getCustomer(utils);
  var customer = formatCustomer(userInfo);
  return customer;
};

/**
 * @title: 定金合并的标点符号
 * @description: 
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-21 17:52
 */
export var getPunctuationMark = function getPunctuationMark(index, totalLength, isEn) {
  var isLastItem = index === totalLength - 1;
  var isSecondLastItem = index === totalLength - 2;
  if (isLastItem) {
    return isEn ? '.' : '。';
  }
  if (isSecondLastItem) {
    return locales.getText('pisell2.appointment.deposit.and');
  }
  return isEn ? ', ' : '、';
};

/**
 * @title: 判断当前选中的资源中是否有 resource_form_id 表单id，如果有表单则下一步是选择表单
 * @description: 
 * @param {any} array
 * @param {any} resources
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-25 19:05
 */
export var hasResourceFormId = function hasResourceFormId(array) {
  try {
    if (!(array !== null && array !== void 0 && array.length)) {
      return false;
    }

    // 检查数组中的任何一个 resource_id 是否满足条件
    return array.some(function (item) {
      var _item$resources;
      // 获取当前项的 resource_id
      var resourceId = item.resource_id;

      // 在 resources 中查找匹配的项
      var foundResource = item === null || item === void 0 || (_item$resources = item.resources) === null || _item$resources === void 0 ? void 0 : _item$resources.find(function (resource) {
        return resource.id === resourceId;
      });

      // 如果找到匹配项并且其 resource_form_id 存在且有值
      return foundResource === null || foundResource === void 0 ? void 0 : foundResource.resource_form_id;
    });
  } catch (error) {
    console.log(error, 'hasResourceFormId');
  }
};
export var formatForms = function formatForms(array) {
  try {
    if (!(array !== null && array !== void 0 && array.length)) {
      return [];
    }
    var mappedResources = array.map(function (item) {
      var _item$resources2;
      // 在 resources 中查找匹配的项
      var foundResource = item === null || item === void 0 || (_item$resources2 = item.resources) === null || _item$resources2 === void 0 ? void 0 : _item$resources2.find(function (resource) {
        return resource.id === item.resource_id;
      });
      if (foundResource && (foundResource !== null && foundResource !== void 0 && foundResource.resource_form_id || foundResource !== null && foundResource !== void 0 && foundResource.policy_id)) {
        return {
          form_id: foundResource.resource_form_id,
          policy_id: foundResource.policy_id,
          id: foundResource === null || foundResource === void 0 ? void 0 : foundResource.id,
          cart_id: item === null || item === void 0 ? void 0 : item._id
        };
      }
      return null;
    }).filter(Boolean);
    return mappedResources;
  } catch (error) {
    console.log(error, 'hasResourceFormId');
    return [];
  }
};

/**
 * @title: 打开表单
 * @description: 
 * @param {string} title
 * @param {any} callback
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-26 15:26
 */
export var openForm = function openForm(_ref) {
  var form_id = _ref.form_id,
    title = _ref.title,
    callback = _ref.callback,
    utils = _ref.utils;
  utils.action({
    type: 'pisell1.form',
    data: {
      form_id: form_id,
      form_title: title,
      isV2API: true
    },
    callback: callback
  });
};

/**
 * @title: 更新数组
 * @description: 
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-26 18:13
 */
export function updateLists(list, key, value, newData) {
  var idx = list.findIndex(function (item) {
    return item[key] === value;
  });
  if (idx > -1) {
    // 存在则合并更新
    var updated = _objectSpread(_objectSpread({}, list[idx]), newData);
    var newList = _toConsumableArray(list);
    newList[idx] = updated;
    return newList;
  } else {
    // 不存在则添加
    return [].concat(_toConsumableArray(list), [_objectSpread(_objectSpread({}, newData), {}, _defineProperty({}, key, value))]);
  }
}

/**
 * @title: 更新表单存储
 * @description: 
 * @param {any} bookingForm
 * @param {number} id
 * @param {any} newData
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-04-27 17:27
 */
export var updateBookingFormStorage = function updateBookingFormStorage(bookingForm, id, newData) {
  // 浅拷贝，避免直接修改原对象
  var newBookingForm = _objectSpread({}, bookingForm);
  if (newBookingForm[id]) {
    // 已存在，合并更新
    newBookingForm[id] = _objectSpread(_objectSpread({}, newBookingForm[id]), newData);
  } else {
    // 不存在，直接添加
    newBookingForm[id] = _objectSpread({}, newData);
  }
  return newBookingForm;
};
export var Toast = function Toast(text) {
  try {
    PisellToast({
      content: text
    });
  } catch (error) {
    var _window;
    (_window = window) === null || _window === void 0 || _window.alert(text);
  }
};

/**
 * @title: 添加或移除表单步骤
 * @description: 
 * @param {any} param1
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-06 21:51
 */
export var checkHasFormStep = function checkHasFormStep(_ref2) {
  var _appointmentBooking$s;
  var setIsErrorHolder = _ref2.setIsErrorHolder,
    nextStep = _ref2.nextStep,
    isSkip = _ref2.isSkip,
    checkHolder = _ref2.checkHolder,
    checkAccount = _ref2.checkAccount,
    carts = _ref2.carts,
    setCarts = _ref2.setCarts,
    hasResourceFormId = _ref2.hasResourceFormId,
    currentStep = _ref2.currentStep,
    stepList = _ref2.stepList,
    appointmentBooking = _ref2.appointmentBooking,
    locales = _ref2.locales,
    isLogin = _ref2.isLogin,
    openLoginModal = _ref2.openLoginModal,
    isFormSubject = _ref2.isFormSubject,
    utils = _ref2.utils,
    accountLists = _ref2.accountLists,
    getHolders = _ref2.getHolders;
  var isNext = false;

  // 验证资源是否挂接了表单
  // if (hasResourceFormId) {
  //   const index = currentStep?.index + 1;
  //   const description = locales.getText('pisell2.appointment.fill-form');

  //   // 判断是否已经存在选择时间步骤
  //   const isExist = stepList?.some((item: any) => item?.key === 'select-time');

  //   appointmentBooking.addStep({ index, key: 'fill-form', description }, isExist ? 'select-time' : undefined);
  // } else {
  //   appointmentBooking.removeStep('fill-form');
  // }

  var stepLists = appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$s = appointmentBooking.store) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.step) === null || _appointmentBooking$s === void 0 || (_appointmentBooking$s = _appointmentBooking$s.store) === null || _appointmentBooking$s === void 0 ? void 0 : _appointmentBooking$s.stepList;

  // 如果当前是倒数第二步，则进行检测登录和账户信息
  // if (currentStep?.index + 2 === stepLists?.length) {
  //   // 检测holder表
  //   const _checkHolder = () => {
  //     const result = checkHolder({
  //       isFormSubject,
  //       checkAccount,
  //       carts,
  //     });

  //     // if (result) {
  //     //   setCarts(result);
  //     // }

  //     if (!!result) return true
  //   }

  //   if (!isLogin) {
  //     utils.action({
  //       type: 'pisell1.login',
  //       data: {
  //         type: 'plugin'
  //       },
  //       callback: (status: string) => {
  //         if (status === 'success') {
  //           if (!isFormSubject) {
  //             const user = JSON.parse(utils?.storage?.get('customer') || '{}');
  //             const currentUser = formatCustomer(user);
  //             const meAccount = accountLists?.find((d: any) => !d?.isGuest);
  //             appointmentBooking.setLoginAccount(meAccount?.id, currentUser);
  //           } else {
  //             getHolders();
  //           }

  //           // 如果只有一个资源执行跳过
  //           if (isSkip) {
  //             isNext = true
  //           }
  //         }
  //       },
  //     });
  //   } else {
  //     isNext = true
  //   }

  //   if (isNext && !_checkHolder()) {
  //     nextStep()
  //   } else {
  //     return true
  //   }
  // } else {
  //   // 如果只有一个资源执行跳过
  //   if (isSkip) {
  //     nextStep()
  //   }
  // }
};
export var getCartCurrentResourceData = function getCartCurrentResourceData(cart, currentKey, resources) {
  try {
    var _currentKey$split, _resources$, _cart$_origin, _resourceItem, _resourceItem2;
    var currentFormId = currentKey === null || currentKey === void 0 || (_currentKey$split = currentKey.split('_@')) === null || _currentKey$split === void 0 || (_currentKey$split = _currentKey$split[1]) === null || _currentKey$split === void 0 ? void 0 : _currentKey$split.split('-');
    var form_id = Number((currentFormId === null || currentFormId === void 0 ? void 0 : currentFormId[0]) || (resources === null || resources === void 0 || (_resources$ = resources[0]) === null || _resources$ === void 0 || (_resources$ = _resources$.resources) === null || _resources$ === void 0 || (_resources$ = _resources$[0]) === null || _resources$ === void 0 ? void 0 : _resources$.id));
    if (!form_id || !resources.length) return;
    var cartId = cart._id;
    var cartOriginResources = ((_cart$_origin = cart._origin) === null || _cart$_origin === void 0 ? void 0 : _cart$_origin.resources) || [];

    // 3. 找到资源数据中与购物车id匹配的资源数据
    var resourceData = resources.find(function (r) {
      return String(r._id) === String(cartId);
    });
    if (!resourceData) return;

    // 4. 在资源数据的 resources 数组中找到当前资源
    var resourceItem = (resourceData.resources || []).find(function (r) {
      return Number(r.id) === form_id;
    });
    if (!resourceItem) return;
    resourceItem = _objectSpread(_objectSpread({}, resourceItem), {}, {
      renderList: ((_resourceItem = resourceItem) === null || _resourceItem === void 0 || (_resourceItem = _resourceItem.renderList) === null || _resourceItem === void 0 ? void 0 : _resourceItem.map(function (r) {
        return _objectSpread(_objectSpread({}, r), {}, {
          label: (r === null || r === void 0 ? void 0 : r.main_field) || (r === null || r === void 0 ? void 0 : r.id)
        });
      })) || []
    });

    // 5. 在购物车的 _origin.resources 里找到当前资源的 render id
    var cartResource = cartOriginResources.find(function (r) {
      return Number(r.form_id) === form_id;
    });

    // 6. 在 resourceItem.renderList 里找到 render id 匹配的那一项
    var renderId = (cartResource === null || cartResource === void 0 ? void 0 : cartResource.id) || 0;

    //  7. renderList 里的 id
    var renderItem = (((_resourceItem2 = resourceItem) === null || _resourceItem2 === void 0 ? void 0 : _resourceItem2.renderList) || []).find(function (r) {
      return Number(r === null || r === void 0 ? void 0 : r.id) === Number(renderId);
    });
    return {
      cartItem: cart,
      resourceItem: _objectSpread(_objectSpread({}, resourceItem), {}, {
        originRenderLists: resourceData.resources
      }),
      renderItem: renderItem
    };
  } catch (error) {
    return cart;
  }
};

/**
 * @title: 获取购物车资源列表
 * @description: 
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-25 13:56
 */
export var getMultiResources = function getMultiResources(cart, currentSelectedResource, currentKey) {
  var _cart$_origin2;
  var resourceItem = currentSelectedResource === null || currentSelectedResource === void 0 ? void 0 : currentSelectedResource.resourceItem;
  if (!resourceItem || !cart) return [];
  var isCurrentResourceStep = currentKey.startsWith('select-resource');
  var resources = (cart === null || cart === void 0 || (_cart$_origin2 = cart._origin) === null || _cart$_origin2 === void 0 ? void 0 : _cart$_origin2.resources) || [];
  // 已选择的资源
  var selectedResources = isCurrentResourceStep ? resources.filter(function (r) {
    return (r === null || r === void 0 ? void 0 : r.form_id) !== (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.form_id);
  }) : resources;
  selectedResources = selectedResources.map(function (r) {
    return _objectSpread(_objectSpread({}, r), {}, {
      name: (r === null || r === void 0 ? void 0 : r.main_field) || (r === null || r === void 0 ? void 0 : r.id),
      isShowChangeResource: false,
      is_hide_like: true
    });
  });
  var currentResources = resources.find(function (r) {
    return (r === null || r === void 0 ? void 0 : r.form_id) === (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.form_id);
  });
  var errorTip = (currentSelectedResource === null || currentSelectedResource === void 0 ? void 0 : currentSelectedResource.errorTip) || {};

  // 当前的资源
  var currentResource = isCurrentResourceStep ? [{
    allowClearResource: true,
    lists: (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.renderList) || [],
    is_hide_like: true,
    id: currentResources === null || currentResources === void 0 ? void 0 : currentResources.id,
    isErrorResource: (errorTip === null || errorTip === void 0 ? void 0 : errorTip.isErrorResource) || false,
    resourceErrorText: (errorTip === null || errorTip === void 0 ? void 0 : errorTip.resourceErrorText) || ''
  }] : [];
  return [].concat(_toConsumableArray(selectedResources), currentResource);
};
export var getCurrentBoard = function getCurrentBoard(appointmentBooking, code) {
  var _appointmentBoard$res;
  var _ref3 = (appointmentBooking === null || appointmentBooking === void 0 ? void 0 : appointmentBooking.otherParams) || {},
    appointmentBoard = _ref3.appointmentBoard;
  var resourceConfig = appointmentBoard === null || appointmentBoard === void 0 || (_appointmentBoard$res = appointmentBoard.resource_tab) === null || _appointmentBoard$res === void 0 ? void 0 : _appointmentBoard$res[code];
  return resourceConfig;
};

/**
 * @title: 
 * @description: 根据资源code获取资源标题
 * @param {any} appointmentBooking
 * @param {string} code
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-24 18:51
 */
export var getResourceTitle = function getResourceTitle(appointmentBooking, code, utils, originResource) {
  var resourceConfig = getCurrentBoard(appointmentBooking, code);
  var name = '';

  // 如果存在配置，则使用配置的名称
  if (resourceConfig !== null && resourceConfig !== void 0 && resourceConfig.resource_type_title) {
    name = utils.translation(resourceConfig === null || resourceConfig === void 0 ? void 0 : resourceConfig.resource_type_title);
  } else {
    name = (originResource === null || originResource === void 0 ? void 0 : originResource.title) || '';
  }
  return name;
};

/**
 * @title: 监测购物车中是否有没有选择资源的
 * @description: 
 * @param {any} carts
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-24 18:47
 */
export var checkAndMarkCartRenderError = function checkAndMarkCartRenderError(carts, appointmentBooking, utils) {
  try {
    var _carts;
    var hasError = false;
    carts = carts.filter(function (n) {
      return !appointmentBooking.isTargetCartIdNormalProduct(n._id);
    });
    var updatedCartList = (_carts = carts) === null || _carts === void 0 ? void 0 : _carts.map(function (cart) {
      var currentResource = cart === null || cart === void 0 ? void 0 : cart.currentSelectedResource;
      if (!(currentResource !== null && currentResource !== void 0 && currentResource.renderItem)) {
        hasError = true;
        var resourceItem = (currentResource === null || currentResource === void 0 ? void 0 : currentResource.resourceItem) || {};
        var name = getResourceTitle(appointmentBooking, resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.code, utils, resourceItem);
        return _objectSpread(_objectSpread({}, cart), {}, {
          currentSelectedResource: _objectSpread(_objectSpread({}, currentResource), {}, {
            errorTip: {
              isErrorResource: true,
              resourceErrorText: name
            }
          })
        });
      }
      return cart;
    });
    return {
      hasError: hasError,
      updatedCartList: updatedCartList
    };
  } catch (error) {
    console.error(error);
    return {};
  }
};

/**
 * @title: 查表表单步骤插入到哪个步骤的后面的key
 * @description: 
 * @param {string} currentKey
 * @param {any} stepList
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-24 19:05
 */
export var getFormInsertBeforeKey = function getFormInsertBeforeKey(currentKey, stepList) {
  // 判断是否第一个资源
  var isFirstResource = currentKey === 'select-resource';

  // 查找“选择时间”步骤
  var timeStep = stepList.find(function (item) {
    return item.key.startsWith('select-time');
  });
  if (isFirstResource && timeStep) {
    // 插入到选择时间后面
    return timeStep.key;
  } else {
    // 插入到当前资源后面
    return currentKey;
  }
};

/**
 * @title: 监测holder数据
 * @description: 
 * @param {any} isFormSubject
 * @param {any} checkAccount
 * @param {any} carts
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-24 22:46
 */
export var checkAndGoNext = function checkAndGoNext(isFormSubject, checkAccount, carts) {
  var hasError = false;
  var updatedCartList = _toConsumableArray(carts);
  if (isFormSubject && !checkAccount('holder')) {
    updatedCartList = carts === null || carts === void 0 ? void 0 : carts.map(function (item) {
      if (!(item !== null && item !== void 0 && item.holder_id)) {
        hasError = true;
        return _objectSpread(_objectSpread({}, item), {}, {
          isErrorHolder: true
        });
      }
      return item;
    });
  }
  return {
    hasError: hasError,
    updatedCartList: updatedCartList
  };
};
export var currentAppointmentBoard = function currentAppointmentBoard(currentKey, resources, appointmentBooking) {
  try {
    var _currentKey$split2, _resources$2, _resources$3, _appointmentBooking$o;
    var currentStepKey = currentKey === null || currentKey === void 0 || (_currentKey$split2 = currentKey.split('_@')) === null || _currentKey$split2 === void 0 || (_currentKey$split2 = _currentKey$split2[1]) === null || _currentKey$split2 === void 0 ? void 0 : _currentKey$split2.split('-');
    var form_id = Number((currentStepKey === null || currentStepKey === void 0 ? void 0 : currentStepKey[0]) || (resources === null || resources === void 0 || (_resources$2 = resources[0]) === null || _resources$2 === void 0 || (_resources$2 = _resources$2.resources) === null || _resources$2 === void 0 || (_resources$2 = _resources$2[0]) === null || _resources$2 === void 0 ? void 0 : _resources$2.id));
    var form_code = (currentStepKey === null || currentStepKey === void 0 ? void 0 : currentStepKey[1]) || (resources === null || resources === void 0 || (_resources$3 = resources[0]) === null || _resources$3 === void 0 || (_resources$3 = _resources$3.resources) === null || _resources$3 === void 0 || (_resources$3 = _resources$3[0]) === null || _resources$3 === void 0 ? void 0 : _resources$3.code);
    var cart_id = currentStepKey === null || currentStepKey === void 0 ? void 0 : currentStepKey[2];
    var resource_form_id = currentStepKey === null || currentStepKey === void 0 ? void 0 : currentStepKey[3];
    var policy_id = currentStepKey === null || currentStepKey === void 0 ? void 0 : currentStepKey[4];
    var resource_tab = appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$o = appointmentBooking.otherParams) === null || _appointmentBooking$o === void 0 || (_appointmentBooking$o = _appointmentBooking$o.appointmentBoard) === null || _appointmentBooking$o === void 0 ? void 0 : _appointmentBooking$o.resource_tab;
    var config = resource_tab === null || resource_tab === void 0 ? void 0 : resource_tab[form_code];
    var isRequired = (config === null || config === void 0 ? void 0 : config.customer_resource) === 'customer_resource_required';
    var isSystem = (config === null || config === void 0 ? void 0 : config.customer_resource) === 'customer_resource_system';
    var isSkip = config !== null && config !== void 0 && config.hasOwnProperty('resource_skip_select') ? !!(config !== null && config !== void 0 && config.resource_skip_select) : true;
    return {
      isRequired: isRequired,
      isSystem: isSystem,
      isSkip: isSkip,
      form_id: form_id,
      form_code: form_code,
      cart_id: cart_id,
      resource_form_id: resource_form_id,
      policy_id: policy_id
    };
  } catch (error) {
    return {};
  }
};
export var currentRenderLists = function currentRenderLists(resources, code) {
  var _resources$4;
  if (!(resources !== null && resources !== void 0 && resources.length) || !code) return [];
  var _resources = (resources === null || resources === void 0 || (_resources$4 = resources[0]) === null || _resources$4 === void 0 ? void 0 : _resources$4.resources) || [];
  var currentResource = _resources === null || _resources === void 0 ? void 0 : _resources.find(function (item) {
    return item.code === code;
  });
  return (currentResource === null || currentResource === void 0 ? void 0 : currentResource.renderList) || [];
};

/**
 * @title: 更新购物车
 * @description: 
 * @param {any} carts
 * @param {any} value
 * @return {*}
 * @Author: WangHan
 * @Date: 2025-05-25 23:17
 */
export var updateCartResources = function updateCartResources(item, value, currentSelectedResources) {
  var _item$_origin;
  var newResource = [];
  var resources = (item === null || item === void 0 || (_item$_origin = item._origin) === null || _item$_origin === void 0 ? void 0 : _item$_origin.resources) || [];

  // 更新资源
  if (value) {
    var currentResourceIndex = resources.findIndex(function (item) {
      return item.form_id === value.form_id;
    });
    if (currentResourceIndex > -1) {
      newResource = resources.map(function (item) {
        return item.form_id === value.form_id ? value : item;
      });
    } else {
      newResource = [].concat(_toConsumableArray(resources), [value]);
    }
  } else {
    var _currentResource$curr;
    // 清空资源
    var currentResource = currentSelectedResources === null || currentSelectedResources === void 0 ? void 0 : currentSelectedResources.find(function (resource) {
      return (resource === null || resource === void 0 ? void 0 : resource._id) === (item === null || item === void 0 ? void 0 : item._id);
    });
    var form_id = currentResource === null || currentResource === void 0 || (_currentResource$curr = currentResource.currentSelectedResource) === null || _currentResource$curr === void 0 || (_currentResource$curr = _currentResource$curr.renderItem) === null || _currentResource$curr === void 0 ? void 0 : _currentResource$curr.form_id;

    // 过滤掉当前资源
    newResource = resources.filter(function (item) {
      return item.form_id !== form_id;
    });
  }
  return newResource || [];
};