import { getSumCapacity } from "../../info2/utilsByBooking";
var formatRelationDetails = function formatRelationDetails(list) {
  return (list || []).map(function (item) {
    return {
      num: item.product_quantity,
      price: item.payment_price || 0,
      product_id: item.product_id,
      is_charge_tax: item.is_charge_tax,
      order_detail_id: item.order_detail_id
    };
  });
};
/**
 * @title: 传入资源，如果有子资源，会根据组合资源的 capacity 计算修改子资源的 capacity
 * @description:
 * @param {object} resource
 * @return {*}
 * @Author: jinglin.tan
 */
export var checkSubResourcesCapacity = function checkSubResourcesCapacity(resource) {
  if (resource.children && resource.children.length) {
    var countCapacity = resource.capacity;
    resource.children.forEach(function (child) {
      if (child.capacity <= countCapacity) {
        countCapacity -= child.capacity;
      } else {
        child.capacity = countCapacity;
        countCapacity = 0;
      }
    });
  }
};
var checkResourcesFormat = function checkResourcesFormat(resources, service, isChild) {
  return resources.map(function (resource) {
    var _resource$children;
    if (!isChild) {
      resource.capacity = getSumCapacity({
        cacheItem: service
      });
    }
    checkSubResourcesCapacity(resource);
    return {
      children: (_resource$children = resource.children) !== null && _resource$children !== void 0 && _resource$children.length ? checkResourcesFormat(resource.children, service, true) : undefined,
      form_id: resource.form_id,
      relation_type: resource.relation_type || 'form',
      relation_id: resource.relation_id || 0,
      capacity: resource.capacity,
      like_status: resource.like_status || 'common',
      metadata: resource.metadata || {}
    };
  });
};
var formatResource = function formatResource(service) {
  if (!service._extend.resource) {
    return undefined;
  }
  try {
    return Object.values(service._extend.resource).reduce(function (pre, cur) {
      var newList = checkResourcesFormat(cur, service);
      pre = pre.concat(newList);
      return pre;
    }, []);
  } catch (err) {
    return undefined;
  }
};

/**
 * @title: 格式化商品数据
 * @description: 
 * @param {any} item
 * @return {*}
 * @Author: WangHan
 * @Date: 2024-12-07 14:21
 */
var formatProducts = function formatProducts(item) {
  return {
    "num": Number(item.detail.quantity),
    "price": Number(item.detail.source_product_price),
    "source_product_price": Number(item.detail.source_product_price),
    "product_id": item.detail.id,
    "discount_amount": item.detail.discount_amount,
    "product_discount_reason": item.detail.product_discount_reason || '',
    "is_charge_tax": item.detail.is_charge_tax,
    "note": item.detail.note,
    "discountway": item.detail.discountway || "num",
    "discount_per": item.detail.discount_per,
    "discount_type": item.detail.discount_type,
    "order_detail_id": item.detail.order_detail_id,
    "product_bundle": item.detail.product_bundle,
    "bundle_edit": 0,
    "product_option_item": [],
    "product_variant_id": item.detail.product_variant_id || 0,
    "discount_list": item.detail.discount_list,
    metadata: item.detail.metadata
  };
};

/**
 * @title: 预约商品信息
 * @description: 
 * @param {any} data
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-02-19 09:26
 */
var formatBookings = function formatBookings(data, values) {
  var bookings = [];
  bookings = data.map(function (item) {
    return {
      "product": formatProducts(item),
      "relation_products": formatRelationDetails(item.relation_details),
      'id': item.id,
      "relation_id": item.relation_id,
      "like_status": item.like_status,
      "relation_type": item.relation_type,
      "is_all": item.is_all,
      "schedule_id": item.schedule_id || 0,
      "end_time": item.end_time,
      "sub_type": item.sub_type,
      "select_date": item.start_date,
      "start_time": item.start_time,
      "end_date": item.end_date,
      "start_date": item.start_date,
      "duration": item.duration,
      "number": item.number,
      "resources": formatResource(item),
      "metadata": item.metadata,
      "holder": item.holder
    };
  });
  return bookings;
};
export var formatValues = function formatValues(data, values) {
  var result = {
    type: "appointment_booking",
    platform: data.platform,
    sales_channel: data.platform,
    bookings: [],
    customer_id: data.customer_id,
    holder: data.holder,
    relation_products: data.relation_products,
    metadata: data.metadata,
    shop_note: data.shop_note,
    shop_discount: data.shop_discount,
    shop_discount_reason: data.shop_discount_reason,
    contacts: data.contacts,
    customer_name: data.customer_name,
    email: data.customer_name,
    phone: data.customer_name,
    country_calling_code: data.customer_name
  };

  // 服务
  result.bookings = formatBookings(data, values);
  return result;
};