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/plus/pisellSalesGrid/serve.ts
var serve_exports = {};
__export(serve_exports, {
  formatBookingResult: () => formatBookingResult,
  getBookingList: () => getBookingList,
  getOrderList: () => getOrderList,
  unsubscribeBookingQuery: () => unsubscribeBookingQuery,
  unsubscribeOrderQuery: () => unsubscribeOrderQuery
});
module.exports = __toCommonJS(serve_exports);
var import_utils = require("../../utils");
var import_dayjs = __toESM(require("dayjs"));
var formatParams = (params) => {
  const { orderDate, total_amount, sort: sort_key, ...rest } = params;
  let order_by, sort;
  if (sort_key) {
    const [orderByValue, sortValue] = sort_key.split(":");
    order_by = orderByValue;
    sort = sortValue;
  }
  return {
    ...rest,
    ...(orderDate == null ? void 0 : orderDate[0]) && {
      start_time: (0, import_dayjs.default)(orderDate[0]).format("YYYY-MM-DD")
    },
    ...(orderDate == null ? void 0 : orderDate[1]) && {
      end_time: (0, import_dayjs.default)(orderDate[1]).format("YYYY-MM-DD")
    },
    ...(total_amount == null ? void 0 : total_amount.min) !== void 0 && {
      min_total_amount: total_amount.min
    },
    ...(total_amount == null ? void 0 : total_amount.max) !== void 0 && {
      max_total_amount: total_amount.max
    },
    ...order_by && { order_by },
    ...sort && { sort },
    with: [
      "customer",
      "contactsInfo",
      "tag",
      "createAccount",
      "payment",
      "lastEditAccount"
    ]
  };
};
function normalizeListPayload(raw) {
  const body = (raw == null ? void 0 : raw.data) ?? raw;
  const payload = (body == null ? void 0 : body.data) ?? body;
  const list = Array.isArray(payload == null ? void 0 : payload.list) ? payload.list : [];
  const count = typeof (payload == null ? void 0 : payload.count) === "number" && Number.isFinite(payload.count) ? payload.count : list.length;
  return {
    ...payload && typeof payload === "object" ? payload : {},
    list,
    count
  };
}
var getOrderList = async (params, options) => {
  const res = await import_utils.request.getRequest().post(`/shop/order/v2/list`, formatParams(params), {
    abort: true,
    fullResult: true,
    isShopApi: true,
    ...options
  });
  return normalizeListPayload(res);
};
var formatBookingParams = (params) => {
  const {
    booking_time_start_between,
    created_at_between,
    updated_at_between,
    number_between,
    total_amount_between,
    sort,
    // `/shop/schedule/booking` 后端字段是 `keywords`（复数）。
    // 这里把通用的 `keyword`（单数，业务/工具栏/外部受控）统一适配到 `keywords`，
    // 让"销售管理"在 monitor（预约）视角下的所有搜索入口（toolbar / 全局搜索 / 外部受控）
    // 都能命中后端搜索条件，避免每个调用方各自重命名。
    keyword,
    keywords,
    ...rest
  } = params;
  const mergedKeywords = keywords ?? keyword;
  const bookingTimeRange = Array.isArray(booking_time_start_between) ? booking_time_start_between.map(
    (value) => value ? (0, import_dayjs.default)(value).format("YYYY-MM-DD HH:mm:ss") : void 0
  ).filter((value) => value) : void 0;
  const createdRange = Array.isArray(created_at_between) ? created_at_between.map(
    (value) => value ? (0, import_dayjs.default)(value).format("YYYY-MM-DD HH:mm:ss") : void 0
  ).filter((value) => value) : void 0;
  const updatedRange = Array.isArray(updated_at_between) ? updated_at_between.map(
    (value) => value ? (0, import_dayjs.default)(value).format("YYYY-MM-DD HH:mm:ss") : void 0
  ).filter((value) => value) : void 0;
  const numberRange = Array.isArray(number_between) ? number_between : (number_between == null ? void 0 : number_between.min) != null || (number_between == null ? void 0 : number_between.max) != null ? [number_between == null ? void 0 : number_between.min, number_between == null ? void 0 : number_between.max] : void 0;
  const amountRange = Array.isArray(total_amount_between) ? total_amount_between : (total_amount_between == null ? void 0 : total_amount_between.min) != null || (total_amount_between == null ? void 0 : total_amount_between.max) != null ? [total_amount_between == null ? void 0 : total_amount_between.min, total_amount_between == null ? void 0 : total_amount_between.max] : void 0;
  let ext = sort === "after_execution_time:asc" ? {
    after_execution_time: (0, import_dayjs.default)().format("YYYY-MM-DD HH:mm:ss")
  } : { order_by: sort };
  return {
    ...rest,
    ...ext,
    ...mergedKeywords !== void 0 && mergedKeywords !== "" && { keywords: mergedKeywords },
    ...(bookingTimeRange == null ? void 0 : bookingTimeRange.length) && {
      sales_time_between: bookingTimeRange,
      sales_time_match: "overlap"
    },
    ...(createdRange == null ? void 0 : createdRange.length) && { created_at_between: createdRange },
    ...(updatedRange == null ? void 0 : updatedRange.length) && { updated_at_between: updatedRange },
    ...numberRange && { number_between: numberRange },
    ...amountRange && { total_amount_between: amountRange },
    with: [
      "order.contactsInfo",
      "order.customer",
      "order.payment",
      "appointmentForm.formRecord",
      // 'children',
      "form",
      "contacts",
      "eventResources.relationForm",
      "notes",
      //
      "holder.formRecord",
      "holderBySelf.formRecord"
    ],
    holder_data: 1,
    child_booking: 1
    // 仅展示子预约
  };
};
var formatBookingData = (data) => {
  const newData = data.map((item) => {
    var _a;
    const product_uid = item == null ? void 0 : item.product_uid;
    const products = Array.isArray((_a = item == null ? void 0 : item.order) == null ? void 0 : _a.products) ? item.order.products : [];
    const currentProduct = products.find(
      (product) => product.booking_uid === product_uid
    );
    const orderBase = (item == null ? void 0 : item.order) != null && typeof item.order === "object" ? item.order : {};
    return {
      ...item,
      order_detail: (item == null ? void 0 : item.order_detail) ?? (currentProduct != null ? JSON.stringify(currentProduct) : void 0),
      id: item.schedule_event_id || item.id,
      order: {
        ...orderBase,
        id: item.order_id || item.order.id
      }
    };
  });
  return newData;
};
var formatBookingResult = (data) => {
  return {
    ...data,
    list: formatBookingData((data == null ? void 0 : data.list) || [])
  };
};
var getBookingList = async (params, options) => {
  const { data } = await import_utils.request.getRequest().get(`/shop/schedule/booking`, formatBookingParams(params), {
    abort: true,
    fullResult: true,
    isShopApi: true,
    ...options
  });
  return formatBookingResult(normalizeListPayload(data));
};
var unsubscribeOrderQuery = async (subscriberId) => {
  await import_utils.request.getRequest().post(
    "/shop/order/v2/list/unsubscribe",
    { subscriberId },
    {
      isShopApi: true,
      osServer: true
    }
  );
};
var unsubscribeBookingQuery = async (subscriberId) => {
  await import_utils.request.getRequest().get(
    "/shop/schedule/booking/unsubscribe",
    { subscriberId },
    {
      isShopApi: true,
      osServer: true
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatBookingResult,
  getBookingList,
  getOrderList,
  unsubscribeBookingQuery,
  unsubscribeOrderQuery
});
