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

  const CHECKED_AT = "2026-07-31";
  const QUANTITY = 10000;

  const SOURCES = Object.freeze({
    factory: Object.freeze({
      id: "factory-cost-library",
      name: "本厂正式成本库",
      url: "",
      scope: "CB-20260730-150602-public-reference；297 条均有价格，启用 296 条，UV详细价格待确认并停用"
    }),
    shengda: Object.freeze({
      id: "shengda",
      name: "盛大印刷",
      url: "https://www.sd2000.com/",
      scope: "登录后同规格客户报价"
    }),
    onlineyin: Object.freeze({
      id: "onlineyin",
      name: "线上印",
      url: "https://www.onlineyin.com/",
      scope: "公开起步价；缺规格、数量或税运口径时只作方向参考"
    }),
    jlcbox: Object.freeze({
      id: "jlcbox",
      name: "嘉立创纸盒",
      url: "https://www.jlc-zh.com/",
      scope: "纸盒 ¥0.15/个起、彩盒 ¥0.20/个起"
    }),
    jlcsmt: Object.freeze({
      id: "jlcsmt",
      name: "嘉立创 SMT",
      url: "https://www.jlc.com/portal/server_guide_42886.html",
      scope: "符合条件时 ¥0.005/焊点；通常 ¥0.01/焊点，工程费另计"
    }),
    chinagoodsBag: Object.freeze({
      id: "chinagoods-nonwoven-bag",
      name: "义乌购无纺布袋供应商",
      url: "https://www.chinagoods.com/store/goodsdetail/1575674369987268610",
      scope: "公开阶梯价 ¥0.45/个（≥1000），规格不完整"
    }),
    chinagoodsAcrylic: Object.freeze({
      id: "chinagoods-acrylic-keychain",
      name: "义乌购亚克力钥匙扣供应商",
      url: "https://www.chinagoods.com/store/goodsdetail/1980567821694513154",
      scope: "公开阶梯价 ¥0.66/个（≥10000），现货随机款"
    }),
    chinagoodsMetal: Object.freeze({
      id: "chinagoods-metal-badge",
      name: "义乌购金属制品供应商",
      url: "https://www.chinagoods.com/store/goodsdetail/185266",
      scope: "公开阶梯价 ¥1.80/个（≥1001），具体尺寸需复核"
    })
  });

  // 单价均为“直接成本”，不含利润、税和运费。组成用于审计，不能替代客户订单的真实规格。
  const PROFILE_MODELS = Object.freeze({
    card: model("90×54mm｜350g铜版纸｜双面四色｜直角", 0.014, 0.018, 0.006, 0.004),
    paper_flat: model("210×285mm｜157g铜版纸｜双面四色｜成品裁切", 0.052, 0.016, 0.004, 0.003),
    book: model("210×285mm｜16P｜封面/内页157g铜版纸｜四色｜骑马钉", 0.420, 0.150, 0.180, 0.050),
    label: model("50×30mm｜铜版纸不干胶｜四色｜覆膜｜模切排废", 0.012, 0.012, 0.014, 0.005),
    paper_box: model("100×60×30mm｜展开300×220mm｜350g白卡｜四色｜覆膜｜模切糊盒", 0.103, 0.055, 0.075, 0.025),
    paper_bag: model("260×360×80mm｜250g白卡｜四色｜覆膜｜模切糊袋｜棉绳", 0.370, 0.080, 0.230, 0.120),
    corrugated: model("300×200×150mm｜三层B楞｜单色印刷｜开槽粘箱", 1.150, 0.120, 0.180, 0.080),
    office: model("210×285mm｜两联无碳纸｜单色｜胶头/打码", 0.085, 0.020, 0.070, 0.025),
    office_folder: model("230×320mm｜250g牛皮纸｜单色｜模切糊制", 0.180, 0.030, 0.060, 0.030),
    calendar: model("210×140mm｜13张250g铜版纸｜四色｜铁圈｜三角架", 1.050, 0.350, 1.100, 0.250),
    display: model("标准展示规格｜高清喷印｜成品裁切/安装辅料", 11.000, 0.800, 1.700, 0.600),
    textile: model("300×350mm｜80g无纺布/常规面料｜单色丝印｜车缝", 0.220, 0.150, 0.120, 0.060),
    hang_tag: model("50×90mm｜350g铜版纸｜四色｜覆膜｜模切打孔", 0.030, 0.020, 0.030, 0.010),
    plastic: model("200×300mm｜常规厚度膜材｜单色印刷｜制袋封边", 0.060, 0.025, 0.030, 0.010),
    gift: model("常规50mm级礼品｜单面彩印/Logo｜独立包装", 0.310, 0.100, 0.180, 0.070),
    industrial: model("100×50mm级部件｜常规材料｜切割/成型｜表面处理", 0.750, 0.080, 0.620, 0.250),
    electronic: model("常规小型电子配件｜基础BOM｜40焊点｜功能测试｜独立包装", 2.400, 0.000, 0.350, 0.150),
    instruction: model("105×145mm｜16P｜80g双胶纸｜单黑｜骑马钉", 0.060, 0.030, 0.020, 0.010)
  });

  function model(spec, material, printing, process, laborEquipment) {
    return Object.freeze({ spec, material, printing, process, laborEquipment });
  }

  const PRODUCT_RULES = Object.freeze([
    rule(/特种纸|透明|哑银|合成纸|反光|双喷|特种纸礼品袋/, 1.45, "特殊材料"),
    rule(/烫金|烫银|金属铭牌|设备标牌/, 1.38, "增加烫印/表面处理"),
    rule(/PVC名片|会员卡|塑胶外壳|硅胶件|硅胶礼品/, 1.85, "非纸基材料"),
    rule(/异形|雕刻|展示架/, 1.55, "异形加工/组装"),
    rule(/可变码|刮刮卡|防伪/, 1.42, "可变数据/特殊涂层"),
    rule(/精装|纪念册|台历礼盒套装/, 3.20, "精装/套装结构"),
    rule(/胶装|菜谱|培训手册|日程本/, 1.55, "胶装与页数增加"),
    rule(/笔记本/, 2.20, "内页与装订增加"),
    rule(/五层/, 1.55, "五层瓦楞"),
    rule(/彩印瓦楞|展示箱/, 1.35, "彩印/展示结构"),
    rule(/酒水|珠宝|书型礼盒|天地盖|抽屉盒/, 2.10, "礼盒级结构与手工"),
    rule(/食品包装盒|化妆品盒|电子产品盒|蛋糕盒/, 1.28, "食品/精品包装工艺"),
    rule(/帆布|棉布|牛津布/, 4.00, "高克重织物"),
    rule(/T恤|工作服/, 12.00, "成衣材料与缝制"),
    rule(/帽子/, 6.00, "帽体与刺绣/印花"),
    rule(/八边封|自立拉链|真空袋|食品复合袋/, 1.80, "复合膜与复杂袋型"),
    rule(/保温杯|木质礼品/, 8.00, "成品载体与加工"),
    rule(/钣金外壳|定制结构件/, 8.00, "结构件材料与多工序"),
    rule(/冲压件|注塑件/, 2.20, "批量成型；模具摊销另列"),
    rule(/数据线|线束组件/, 1.35, "线材与端子"),
    rule(/PCBA|电子模块/, 2.80, "BOM与贴装"),
    rule(/小家电组装/, 6.00, "整机BOM与组装测试"),
    rule(/易拉宝|门型展架|X展架/, 1.00, "画面与标准支架"),
    rule(/亚克力展板|灯箱片|标识标牌/, 1.80, "板材/灯片与加工")
  ]);

  const EXACT_MARKET_BENCHMARKS = Object.freeze({
    "books-01": Object.freeze({
      unitPrice: 7279 / QUANTITY,
      specification: "16P｜210×285mm｜封面/内页157g铜版纸｜四色｜骑马钉",
      comparability: "同规格1万件客户报价"
    }),
    "flyers-01": Object.freeze({
      unitPrice: 923.4 / QUANTITY,
      specification: "210×285mm｜157g铜版纸｜双面｜彩色｜成品裁切",
      comparability: "同规格1万张客户报价；页面总价¥923.40、单价¥0.0924/张"
    })
  });

  function rule(pattern, multiplier, note) {
    return Object.freeze({ pattern, multiplier, note });
  }

  function round(value, digits = 6) {
    const factor = 10 ** digits;
    return Math.round((Number(value) + Number.EPSILON) * factor) / factor;
  }

  function productAdjustment(name) {
    const matched = PRODUCT_RULES.filter(item => item.pattern.test(String(name || "")));
    return Object.freeze({
      multiplier: round(matched.reduce((value, item) => value * item.multiplier, 1), 4),
      notes: Object.freeze(matched.map(item => item.note))
    });
  }

  function marketReference(product) {
    const name = String(product?.name || product?.product || "");
    const profile = String(product?.profile || "");
    const exact = EXACT_MARKET_BENCHMARKS[String(product?.id || "")];
    if (exact) {
      return market("exact", exact.unitPrice, SOURCES.shengda, exact.specification, exact.comparability);
    }
    if (/亚克力钥匙扣/.test(name)) {
      return market("quantity_tier", 0.66, SOURCES.chinagoodsAcrylic, "现货亚克力文字牌钥匙扣，随机混发", "数量相同、规格不完整");
    }
    if (/无纺布袋/.test(name)) {
      return market("quantity_tier", 0.45, SOURCES.chinagoodsBag, "无纺布定制袋公开阶梯", "数量覆盖1万件、规格不完整");
    }
    if (/金属徽章/.test(name)) {
      return market("quantity_tier", 1.80, SOURCES.chinagoodsMetal, "金属徽章/奖牌公开阶梯", "数量覆盖1万件、尺寸需复核");
    }
    if (profile === "label") {
      return market("starting_unit", 0.05, SOURCES.onlineyin, "不干胶 ¥0.05/个起", "起步价；不得视为同规格成交价");
    }
    if (profile === "paper_box" || /杯套|汉堡盒|炸鸡盒|蛋糕盒/.test(name)) {
      return market("starting_unit", 0.20, SOURCES.jlcbox, "彩盒 ¥0.20/个起", "起步价；结构、尺寸、材料未对齐");
    }
    if (profile === "corrugated") {
      return market("starting_unit", 0.15, SOURCES.jlcbox, "纸盒 ¥0.15/个起", "起步价；楞型、尺寸、印色未对齐");
    }
    if (/易拉宝/.test(name)) return market("starting_unit", 25, SOURCES.onlineyin, "易拉宝 ¥25/套起", "起步价；画面和支架规格未对齐");
    if (/门型展架/.test(name)) return market("starting_unit", 19, SOURCES.onlineyin, "门型展架 ¥19/套起", "起步价；画面和支架规格未对齐");
    if (/车贴/.test(name)) return market("starting_unit", 6.5, SOURCES.onlineyin, "车贴 ¥6.5/㎡起", "按标准测试面积1㎡方向比较");
    if (/喷绘布/.test(name)) return market("starting_unit", 8, SOURCES.onlineyin, "喷绘布 ¥8/㎡起", "按标准测试面积1㎡方向比较");
    if (/鼠标垫/.test(name)) return market("starting_unit", 1, SOURCES.onlineyin, "鼠标垫 ¥1/个起", "起步价；尺寸、厚度、印刷未对齐");
    if (profile === "electronic") {
      return Object.freeze({
        kind: "formula_only",
        unitPrice: null,
        totalPrice: null,
        source: SOURCES.jlcsmt,
        publicText: "符合条件 ¥0.005/焊点；常规 ¥0.01/焊点；工程费/BOM另计",
        comparability: "仅加工公式，不含BOM，不能与整件成本直接计算差额"
      });
    }
    if (profile === "card") {
      return Object.freeze({
        kind: "starting_batch",
        unitPrice: null,
        totalPrice: null,
        source: SOURCES.onlineyin,
        publicText: "铜版纸名片 ¥6.66 起",
        comparability: "未公开对应数量与完整规格，不能外推到1万件"
      });
    }
    if (profile === "paper_flat") {
      return Object.freeze({
        kind: "starting_batch",
        unitPrice: null,
        totalPrice: null,
        source: SOURCES.onlineyin,
        publicText: "宣传单 ¥69 起",
        comparability: "未公开对应数量与完整规格，不能外推到1万件"
      });
    }
    return Object.freeze({
      kind: "no_public_price",
      unitPrice: null,
      totalPrice: null,
      source: SOURCES.factory,
      publicText: "未发现可审计的同规格1万件公开价",
      comparability: "已计算软件直接成本；同行价需登录配置或供应商按标准测试规格回价"
    });
  }

  function market(kind, unitPrice, source, publicText, comparability) {
    return Object.freeze({
      kind,
      unitPrice: round(unitPrice),
      totalPrice: round(unitPrice * QUANTITY, 2),
      source,
      publicText,
      comparability
    });
  }

  function benchmarkFor(product) {
    const profile = String(product?.profile || "");
    const base = PROFILE_MODELS[profile];
    if (!base) return null;
    const adjustment = productAdjustment(product?.name);
    const exactBook = product?.id === "books-01";
    const unit = exactBook
      ? Object.freeze({ material: 0.4865647656165, printing: 0.136962, process: 0.18, laborEquipment: 0 })
      : Object.freeze({
        material: round(base.material * adjustment.multiplier),
        printing: round(base.printing * adjustment.multiplier),
        process: round(base.process * adjustment.multiplier),
        laborEquipment: round(base.laborEquipment * adjustment.multiplier)
      });
    const unitTotal = round(unit.material + unit.printing + unit.process + unit.laborEquipment);
    const total = round(unitTotal * QUANTITY, 2);
    const marketData = marketReference(product);
    return Object.freeze({
      schemaVersion: 1,
      productId: String(product?.id || ""),
      productName: String(product?.name || ""),
      profile,
      quantity: QUANTITY,
      specification: `${product?.name || "产品"}｜${base.spec}｜不含利润/税/运`,
      costMethod: exactBook ? "正式成本库+精细书刊拼版/印张公式" : "正式成本库+产品级标准测试规格成本工程模型",
      costEvidence: exactBook ? "factory_confirmed_and_formula" : "public_reference_pending_factory_confirmation",
      adjustmentNotes: adjustment.notes,
      unitBreakdown: unit,
      unitDirectCost: unitTotal,
      totalDirectCost: total,
      market: marketData,
      checkedAt: CHECKED_AT,
      autoApplyToCustomerPrice: false
    });
  }

  function catalogRows(catalog) {
    const categories = Array.isArray(catalog?.categories) ? catalog.categories : [];
    return Object.freeze(categories.flatMap(category => (
      (category.products || []).map(product => Object.freeze({
        categoryId: category.id,
        categoryName: category.name,
        ...benchmarkFor(product)
      }))
    )));
  }

  function benchmarkByName(name, catalog) {
    const needle = String(name || "").trim();
    if (!needle) return null;
    return catalogRows(catalog).find(item => item.productName === needle) || null;
  }

  return Object.freeze({
    schemaVersion: 1,
    checkedAt: CHECKED_AT,
    quantity: QUANTITY,
    sources: SOURCES,
    profileModels: PROFILE_MODELS,
    benchmarkFor,
    benchmarkByName,
    catalogRows
  });
});
