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/server/modules/order/utils/filterOrders.ts
var filterOrders_exports = {};
__export(filterOrders_exports, {
  filterOrders: () => filterOrders
});
module.exports = __toCommonJS(filterOrders_exports);
var import_dayjs = __toESM(require("dayjs"));
function getOrderPaymentCodes(order) {
  const payments = order.payments;
  if (!Array.isArray(payments) || payments.length === 0) {
    return [];
  }
  return payments.map((item) => item == null ? void 0 : item.code).filter((code) => typeof code === "string" && code.length > 0);
}
function filterOrders(orders, filters) {
  const safeFilters = filters || {};
  if (!orders || !Array.isArray(orders)) {
    return {
      list: [],
      count: 0,
      size: 0,
      skip: 0,
      rejected: []
    };
  }
  const size = safeFilters.num || 20;
  const page = safeFilters.skip || 1;
  const rejected = [];
  let filteredList = orders.filter((order) => {
    if (safeFilters.keyword) {
      if (!JSON.stringify(order).toLowerCase().includes(safeFilters.keyword.toLowerCase())) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "keyword",
            actual: "",
            expected: safeFilters.keyword
          }
        });
        return false;
      }
    }
    if (safeFilters.status && safeFilters.status.length > 0) {
      if (!order.status || !safeFilters.status.includes(order.status)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "status",
            actual: order.status,
            expected: safeFilters.status
          }
        });
        return false;
      }
    }
    if (safeFilters.payment_status && safeFilters.payment_status.length > 0) {
      if (!order.payment_status || !safeFilters.payment_status.includes(order.payment_status)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "payment_status",
            actual: order.payment_status,
            expected: safeFilters.payment_status
          }
        });
        return false;
      }
    }
    if (safeFilters.shipping_status && safeFilters.shipping_status.length > 0) {
      if (!order.shipping_status || !safeFilters.shipping_status.includes(order.shipping_status)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "shipping_status",
            actual: order.shipping_status,
            expected: safeFilters.shipping_status
          }
        });
        return false;
      }
    }
    if (safeFilters.customer_id !== void 0 && safeFilters.customer_id !== null) {
      if (String(order.customer_id) !== String(safeFilters.customer_id)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "customer_id",
            actual: order.customer_id,
            expected: safeFilters.customer_id
          }
        });
        return false;
      }
    }
    if (safeFilters.order_sales_channel && safeFilters.order_sales_channel.length > 0) {
      if (!order.order_sales_channel || !safeFilters.order_sales_channel.includes(order.order_sales_channel)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "order_sales_channel",
            actual: order.order_sales_channel,
            expected: safeFilters.order_sales_channel
          }
        });
        return false;
      }
    }
    if (safeFilters.payment_methods && safeFilters.payment_methods.length > 0) {
      const orderPaymentCodes = getOrderPaymentCodes(order);
      if (orderPaymentCodes.length === 0) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "payment_methods",
            actual: order.payments,
            expected: safeFilters.payment_methods
          }
        });
        return false;
      }
      const hasMatch = orderPaymentCodes.some((code) => safeFilters.payment_methods.includes(code));
      if (!hasMatch) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "payment_methods",
            actual: orderPaymentCodes,
            expected: safeFilters.payment_methods
          }
        });
        return false;
      }
    }
    if (safeFilters.min_total_amount !== void 0 || safeFilters.max_total_amount !== void 0) {
      const orderAmount = parseFloat(String(order.total_amount || 0));
      if (safeFilters.min_total_amount !== void 0 && orderAmount < safeFilters.min_total_amount) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "min_total_amount",
            actual: order.total_amount,
            expected: safeFilters.min_total_amount
          }
        });
        return false;
      }
      if (safeFilters.max_total_amount !== void 0 && orderAmount > safeFilters.max_total_amount) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "max_total_amount",
            actual: order.total_amount,
            expected: safeFilters.max_total_amount
          }
        });
        return false;
      }
    }
    if (safeFilters.tag_ids && safeFilters.tag_ids.length > 0) {
      if (!order.tag_ids || !Array.isArray(order.tag_ids) || order.tag_ids.length === 0) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "tag_ids",
            actual: order.tag_ids,
            expected: safeFilters.tag_ids
          }
        });
        return false;
      }
      const filterTagSet = new Set(safeFilters.tag_ids.map(String));
      const hasMatch = order.tag_ids.some((tag) => filterTagSet.has(String(tag)));
      if (!hasMatch) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "tag_ids",
            actual: order.tag_ids,
            expected: safeFilters.tag_ids
          }
        });
        return false;
      }
    }
    if (safeFilters.shipping_type && safeFilters.shipping_type.length > 0) {
      if (!order.shipping_type || !safeFilters.shipping_type.includes(order.shipping_type)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "shipping_type",
            actual: order.shipping_type,
            expected: safeFilters.shipping_type
          }
        });
        return false;
      }
    }
    if (safeFilters.delivery_zone_name !== void 0 && safeFilters.delivery_zone_name !== null && safeFilters.delivery_zone_name !== "") {
      if (!order.delivery_zone_name || !order.delivery_zone_name.toLowerCase().includes(safeFilters.delivery_zone_name.toLowerCase())) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "delivery_zone_name",
            actual: order.delivery_zone_name,
            expected: safeFilters.delivery_zone_name
          }
        });
        return false;
      }
    }
    if (safeFilters.location_id !== void 0 && safeFilters.location_id !== null) {
      if (String(order.location_id) !== String(safeFilters.location_id)) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "location_id",
            actual: order.location_id,
            expected: safeFilters.location_id
          }
        });
        return false;
      }
    }
    if (safeFilters.shipping_order_number !== void 0 && safeFilters.shipping_order_number !== null && safeFilters.shipping_order_number !== "") {
      if (!order.shipping_order_number || !order.shipping_order_number.toLowerCase().includes(safeFilters.shipping_order_number.toLowerCase())) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "shipping_order_number",
            actual: order.shipping_order_number,
            expected: safeFilters.shipping_order_number
          }
        });
        return false;
      }
    }
    if (safeFilters.logistics_company_id && safeFilters.logistics_company_id.length > 0) {
      if (!order.logistics_company_id) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "logistics_company_id",
            actual: order.logistics_company_id,
            expected: safeFilters.logistics_company_id
          }
        });
        return false;
      }
      const orderLogisticsId = String(order.logistics_company_id);
      const hasMatch = safeFilters.logistics_company_id.some((id) => String(id) === orderLogisticsId);
      if (!hasMatch) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "logistics_company_id",
            actual: order.logistics_company_id,
            expected: safeFilters.logistics_company_id
          }
        });
        return false;
      }
    }
    if (safeFilters.zip !== void 0 && safeFilters.zip !== null && safeFilters.zip !== "") {
      if (!order.zip || !order.zip.toLowerCase().includes(safeFilters.zip.toLowerCase())) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "zip",
            actual: order.zip,
            expected: safeFilters.zip
          }
        });
        return false;
      }
    }
    if (safeFilters.business_code) {
      if (!order.business_code || order.business_code !== safeFilters.business_code) {
        rejected.push({
          order_id: order.order_id,
          order_number: order.order_number,
          reason: {
            field: "business_code",
            actual: order.business_code,
            expected: safeFilters.business_code
          }
        });
        return false;
      }
    }
    if (safeFilters.start_time || safeFilters.end_time) {
      const orderTime = order.created_at ? typeof order.created_at === "number" ? order.created_at : (0, import_dayjs.default)(String(order.created_at)).valueOf() : 0;
      if (safeFilters.start_time) {
        const startTs = (0, import_dayjs.default)(safeFilters.start_time).startOf("day").valueOf();
        if (orderTime < startTs) {
          rejected.push({
            order_id: order.order_id,
            order_number: order.order_number,
            reason: {
              field: "start_time",
              actual: order.created_at,
              expected: safeFilters.start_time
            }
          });
          return false;
        }
      }
      if (safeFilters.end_time) {
        const endTs = (0, import_dayjs.default)(safeFilters.end_time).endOf("day").valueOf();
        if (orderTime > endTs) {
          rejected.push({
            order_id: order.order_id,
            order_number: order.order_number,
            reason: {
              field: "end_time",
              actual: order.created_at,
              expected: safeFilters.end_time
            }
          });
          return false;
        }
      }
    }
    return true;
  });
  if (safeFilters.order_by) {
    const sortField = safeFilters.order_by;
    const sortOrder = safeFilters.sort || "desc";
    filteredList.sort((a, b) => {
      let aVal;
      let bVal;
      switch (sortField) {
        case "created_at":
          aVal = a.created_at ? typeof a.created_at === "number" ? a.created_at : new Date(String(a.created_at)).getTime() : 0;
          bVal = b.created_at ? typeof b.created_at === "number" ? b.created_at : new Date(String(b.created_at)).getTime() : 0;
          break;
        case "updated_at":
          aVal = a.updated_at ? typeof a.updated_at === "number" ? a.updated_at : new Date(String(a.updated_at)).getTime() : 0;
          bVal = b.updated_at ? typeof b.updated_at === "number" ? b.updated_at : new Date(String(b.updated_at)).getTime() : 0;
          break;
        case "total_amount":
          aVal = parseFloat(String(a.total_amount || 0));
          bVal = parseFloat(String(b.total_amount || 0));
          break;
        case "customer_name":
          aVal = (a.customer_name || "").toLowerCase();
          bVal = (b.customer_name || "").toLowerCase();
          break;
        case "amount_gap":
          return 0;
        default:
          return 0;
      }
      if (aVal < bVal) {
        return sortOrder === "asc" ? -1 : 1;
      }
      if (aVal > bVal) {
        return sortOrder === "asc" ? 1 : -1;
      }
      return 0;
    });
  }
  const totalCount = filteredList.length;
  const startIndex = (page - 1) * size;
  const endIndex = startIndex + size;
  const paginatedList = filteredList.slice(startIndex, endIndex);
  return {
    list: paginatedList,
    count: totalCount,
    size,
    skip: page,
    rejected
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  filterOrders
});
