(function (root, factory) {
  "use strict";
  const api = factory(root);
  if (typeof module === "object" && module.exports) module.exports = api;
  if (root) root.JIANGLAI_QUOTE_MARKET_BENCHMARKS = Object.freeze(api);
})(typeof globalThis !== "undefined" ? globalThis : this, function (root) {
  "use strict";

  const CHECKED_AT = "2026-07-30";
  const CHANNELS = Object.freeze([
    Object.freeze({ id:"shengda", name:"盛大印刷", url:"https://www.sd2000.com/", scope:"纸品、名片、画册、纸袋", quoteMode:"登录后在线规格报价", priceScope:"客户成交参考；促销和运费单独标记" }),
    Object.freeze({ id:"feiyin", name:"飞印网", url:"https://www.92mp.com/", scope:"纸品、名片、画册、标签、办公与展示", quoteMode:"在线规格报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"onlineyin", name:"线上印", url:"https://www.onlineyin.com/", scope:"商务印刷、标签、办公与礼品", quoteMode:"在线报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"linyeah", name:"灵燕印刷", url:"https://www.linyeah.com/", scope:"小批量数码与商务印刷", quoteMode:"在线配置报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"qin", name:"QinPrinting", url:"https://www.qinprinting.com/quote/", scope:"书刊、包装、纸品", quoteMode:"在线询价", priceScope:"上海工厂 EXW；不含运费、税费和关税" }),
    Object.freeze({ id:"jlcbox", name:"嘉立创纸盒", url:"https://www.jlc-zh.com/", scope:"纸盒与纸板包装", quoteMode:"在线报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"yinbq", name:"尊彩标签", url:"https://www.yinbq.com/", scope:"不干胶与标签", quoteMode:"登录后在线报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"box1688", name:"1688Box", url:"https://1688box.com/", scope:"包装盒与纸箱", quoteMode:"在线报价", priceScope:"客户报价参考" }),
    Object.freeze({ id:"jlcsmt", name:"嘉立创 PCB/SMT", url:"https://smt.szlcsc.com/", scope:"电子 PCB/SMT", quoteMode:"在线报价", priceScope:"客户报价参考" })
  ]);

  const EXACT_BENCHMARKS = Object.freeze([
    Object.freeze({
      id:"shengda-book-16p-285x210-157g", channelId:"shengda", productKind:"book",
      productLabel:"企业画册 16P", size:"285×210", material:"157g铜版纸封面/内页",
      quantities:Object.freeze({ 500:677, 1000:935, 2000:1665, 3000:2348, 5000:3769, 10000:7279 }),
      priceType:"客户报价", taxScope:"以页面口径为准", freightIncluded:false, promotional:false, checkedAt:CHECKED_AT
    }),
    Object.freeze({
      id:"shengda-card-90x54-350g-1000", channelId:"shengda", productKind:"card",
      productLabel:"标准名片", size:"90×54", material:"350g纸，双面",
      quantities:Object.freeze({ 1000:40 }), priceType:"促销客户报价", taxScope:"以页面口径为准",
      freightIncluded:false, knownFreight:4.2, promotional:true, checkedAt:CHECKED_AT
    })
  ]);

  function text(value) {
    return String(value || "").trim().toLocaleLowerCase().replace(/\s+/g, "");
  }

  function normalizeSize(value) {
    const numbers = String(value || "").match(/\d+(?:\.\d+)?/g);
    if (!numbers || numbers.length < 2) return "";
    return numbers.slice(0, 2).map(Number).sort((left, right) => left - right).join("x");
  }

  function productKind(input) {
    const value = text([input?.product, input?.configurationText].filter(Boolean).join(" "));
    if (/名片|卡片/.test(value)) return "card";
    if (/画册|书刊|图册|样本/.test(value)) return "book";
    if (/标签|不干胶/.test(value)) return "label";
    if (/纸箱|瓦楞/.test(value)) return "corrugated";
    if (/纸盒|彩盒|包装盒/.test(value)) return "paper_box";
    if (/纸袋|手提袋/.test(value)) return "paper_bag";
    if (/pcb|smt|电子/.test(value)) return "electronic";
    return "paper_print";
  }

  function channel(channelId) {
    return CHANNELS.find(item => item.id === channelId) || null;
  }

  function relevantChannels(input) {
    const kind = productKind(input);
    const ids = kind === "electronic" ? ["jlcsmt"]
      : kind === "label" ? ["yinbq", "onlineyin", "linyeah"]
        : kind === "paper_box" || kind === "corrugated" ? ["jlcbox", "box1688", "qin"]
          : kind === "paper_bag" ? ["shengda", "qin", "box1688"]
            : kind === "book" ? ["shengda", "feiyin", "onlineyin", "linyeah", "qin"]
              : ["shengda", "feiyin", "onlineyin", "linyeah", "qin"];
    return ids.map(channel).filter(Boolean);
  }

  function exactBenchmark(input) {
    const kind = productKind(input);
    const quantity = Number(input?.quantity || 0);
    const size = normalizeSize(input?.size);
    const configuration = String(input?.configurationText || "");
    return EXACT_BENCHMARKS.find(item => {
      if (item.productKind !== kind || !Object.prototype.hasOwnProperty.call(item.quantities, quantity)) return false;
      if (normalizeSize(item.size) !== size) return false;
      if (kind === "book" && !/(^|[^0-9])16\s*[p页](?:$|[^0-9])/i.test(configuration)) return false;
      return true;
    }) || null;
  }

  function standardBenchmark(input) {
    const engine = root?.JIANGLAI_QUOTE_PRODUCT_BENCHMARKS;
    const catalog = root?.QUOTE_PRODUCT_CATALOG;
    if (!engine?.benchmarkByName || !catalog || Number(input?.quantity || 0) !== 10000) return null;
    return engine.benchmarkByName(input?.product, catalog);
  }

  function compare(input) {
    const benchmark = exactBenchmark(input);
    const channels = relevantChannels(input);
    if (!benchmark) {
      const standard = standardBenchmark(input);
      const marketTotal = Number(standard?.market?.totalPrice || 0);
      const internalCost = Number(input?.internalCost || 0);
      return Object.freeze({
        exact:false,
        directional:Boolean(standard),
        checkedAt:CHECKED_AT,
        standard,
        market:standard?.market || null,
        internalCost,
        marketTotal:marketTotal || null,
        difference:marketTotal ? internalCost - marketTotal : null,
        percent:marketTotal ? (internalCost - marketTotal) / marketTotal : null,
        channels:Object.freeze(channels),
        autoApplied:false
      });
    }
    const marketTotal = Number(benchmark.quantities[Number(input.quantity)]);
    const internalCost = Number(input?.internalCost || 0);
    const difference = internalCost - marketTotal;
    return Object.freeze({
      exact:true,
      checkedAt:CHECKED_AT,
      benchmark,
      channel:channel(benchmark.channelId),
      internalCost,
      marketTotal,
      difference,
      percent:marketTotal > 0 ? difference / marketTotal : 0,
      channels:Object.freeze(channels),
      autoApplied:false
    });
  }

  return Object.freeze({
    checkedAt: CHECKED_AT,
    channels: CHANNELS,
    exactBenchmarks: EXACT_BENCHMARKS,
    normalizeSize,
    productKind,
    relevantChannels,
    exactBenchmark,
    standardBenchmark,
    compare
  });
});
