import dayjs from 'dayjs';
/**
 * 格式化资源到购物车
 * @param resource 资源
 * @returns 格式化后的资源
 */
export var formatResourceToCartItem = function formatResourceToCartItem(params) {
  var cartItem = params.cartItem,
    resources = params.resources;
  if (resources && resources !== null && resources !== void 0 && resources.length) {
    var firstResource = resources[0];
    cartItem.resource_id = firstResource === null || firstResource === void 0 ? void 0 : firstResource.id;
    cartItem.relation_form_name = firstResource === null || firstResource === void 0 ? void 0 : firstResource.main_field;
    if (firstResource.startTime && firstResource.endTime) {
      cartItem.start_date = dayjs(firstResource.startTime).format('YYYY-MM-DD');
      cartItem.start_time = dayjs(firstResource.startTime).format('HH:mm');
      cartItem.end_date = dayjs(firstResource.endTime).format('YYYY-MM-DD');
      cartItem.end_time = dayjs(firstResource.endTime).format('HH:mm');
    }
  } else {
    cartItem.resource_id = undefined;
    cartItem.relation_form_name = undefined;
    cartItem.start_date = undefined;
    cartItem.start_time = undefined;
    cartItem.end_date = undefined;
    cartItem.end_time = undefined;
  }
  var oringin = formatResourceToCartItemOrigin(params);
  cartItem._origin = oringin;
  cartItem._resourceOrigin = resources;
  return cartItem;
};
export var formatResourceToCartItemOrigin = function formatResourceToCartItemOrigin(params) {
  var cartItem = params.cartItem,
    resources = params.resources;
  var origin = cartItem._origin;
  if (resources && resources !== null && resources !== void 0 && resources.length) {
    origin.resources = [];
    // 接口只需要部分字段，需要处理
    var checkResourcesFormat = function checkResourcesFormat(resources, arr) {
      resources.forEach(function (resource) {
        var _resource$children;
        var childArr = [];
        if (resource !== null && resource !== void 0 && (_resource$children = resource.children) !== null && _resource$children !== void 0 && _resource$children.length) {
          checkResourcesFormat(resource.children, childArr);
        }
        var obj = {
          relation_type: 'form',
          like_status: resource.like_status || 'common',
          id: resource.id,
          main_field: resource.main_field,
          resourceType: resource.resourceType,
          form_id: resource.form_id,
          relation_id: resource.id,
          capacity: (resource === null || resource === void 0 ? void 0 : resource.capacity) || 0,
          metadata: (resource === null || resource === void 0 ? void 0 : resource.metadata) || {} // 后端可以在这里挂一个前端用于计算的数据
        };
        if (childArr.length) {
          obj.children = childArr;
        }
        arr.push(obj);
      });
    };
    checkResourcesFormat(resources, origin.resources);
    var _resources$ = resources[0],
      startTime = _resources$.startTime,
      endTime = _resources$.endTime;
    if (startTime && endTime) {
      origin.select_date = dayjs(startTime).format('YYYY-MM-DD');
      origin.start_date = dayjs(startTime).format('YYYY-MM-DD');
      origin.start_time = dayjs(startTime).format('HH:mm');
      origin.end_date = dayjs(endTime).format('YYYY-MM-DD');
      origin.end_time = dayjs(endTime).format('HH:mm');
    }
  } else if ((resources === null || resources === void 0 ? void 0 : resources.length) === 0) {
    origin.resources = [];
    origin.select_date = null;
    origin.start_date = null;
    origin.start_time = null;
    origin.end_date = null;
    origin.end_time = null;
  }
  return origin;
};

/**
 * 从购物车中删除资源相关信息
 * @param cartItem 购物车
 * @returns 删除后的购物车
 */
export var deleteResourceFromCartItem = function deleteResourceFromCartItem(cartItem) {
  // 删除UI层的数据
  cartItem.resource_id = undefined;
  cartItem.relation_form_name = undefined;

  // 删除资源原始数据
  delete cartItem._resourceOrigin;

  // 删除原始数据
  cartItem._origin.resources = null;
  return cartItem;
};