/**
 * 两盘三盘合参：年长日主流年→流时定格到二、三盘；产生日同理。
 * 运行：node website/app/js/kr_hecan_selftest.js
 */
(function () {
  "use strict";
  var standalone = typeof module !== "undefined" && require.main === module;
  var fails = 0;
  function ok(name, cond) {
    if (!cond) { fails++; console.error("FAIL", name); }
    else console.log("ok", name);
  }
  function eq(name, a, b) {
    if (JSON.stringify(a) !== JSON.stringify(b)) { fails++; console.error("FAIL", name, "got", a, "want", b); }
    else console.log("ok", name);
  }

  if (!global.KRParams) require("./kr_params_2026_08_11.js");
  if (!global.KRRelations) require("./kr_relations.js");
  if (!global.KRXinGong) require("./kr_xin_gong.js");
  if (!global.KREngine) require("./kr_engine.js");
  if (!global.KRHecan) require("./kr_hecan.js");

  var H = global.KRHecan;
  var father = {
    name: "泽森", birthMs: Date.UTC(1973, 9, 15, 12),
    pillars: { year: "癸丑", month: "壬戌", day: "甲申", time: "庚午" },
    lat: 23.13, lon: 113.26, hour: 12, minute: 0, sex: "M",
  };
  var son = {
    name: "儿子", birthMs: Date.UTC(2008, 11, 3, 2),
    pillars: { year: "戊子", month: "癸亥", day: "戊寅", time: "癸丑" },
    lat: 23.13, lon: 113.26, hour: 2, minute: 0, sex: "M",
  };
  var wife = {
    name: "妻子", birthMs: Date.UTC(1974, 1, 12, 12),
    pillars: { year: "甲寅", month: "丙寅", day: "庚子", time: "癸未" },
    lat: 23.13, lon: 113.26, hour: 12, minute: 0, sex: "F",
  };
  var ev = { y: 2023, mo: 3, d: 17, h: 12, mi: 0 };
  var ev2 = { y: 2026, mo: 6, d: 6, h: 12, mi: 0 };

  var flow = H.flowPillarsAt(ev, 113.26, 23.13, 8);
  ok("产生日排出流年→流时", !!(flow && flow.year && flow.month && flow.day && flow.time));
  ok("2023-03-17 流年癸卯", flow.year === "癸卯");

  var flow2 = H.flowPillarsAt(ev2, 113.26, 23.13, 8);
  ok("另一产生日流年不同", flow2.year !== flow.year && flow2.year === "丙午");

  var dual = H.dualHecan(son, father, { eventDt: ev, lat: 23.13, lon: 113.26, zone: 8 });
  ok("乱序仍以年长为钟", dual.selfLabel === "泽森" && dual.otherLabel === "儿子");
  ok("定格流柱即产生日流柱", dual.flow.year === flow.year && dual.flow.time === flow.time);
  ok("儿子被定格", dual.juniors.length === 1 && dual.juniors[0].person.name === "儿子");
  eq("定格 extras 为流年→流时", dual.extras.map(function (e) { return e.label; }), ["流年", "流月", "流日", "流时"]);
  ok("年长正从不改", dual.self.zheng.pattern === "正格" && dual.self.zheng.strength === "身弱");
  var sonNatal = H.analyzePerson(son);
  var sonStamp = dual.juniors[0];
  ok("被定格盘正从身份不改", sonStamp.natal.zheng.pattern === sonNatal.zheng.pattern &&
    sonStamp.natal.zheng.strength === sonNatal.zheng.strength &&
    sonStamp.stamped.zheng.pattern === sonNatal.zheng.pattern);
  ok("断语含定格到二盘", /定格儿子/.test(dual.methodLineZh) && /产生日/.test(dual.headlineZh) && /定格到儿子来作用/.test(dual.hecanNotes.join("\n")));
  ok("口径一句在场", /年长日主的流年→流时/.test(dual.canonZh) && /产生日同理/.test(dual.canonZh));

  var triple = H.tripleHecan([son, wife, father], { eventDt: ev, lat: 23.13, lon: 113.26, zone: 8 });
  ok("三盘年长仍为父", triple.selfLabel === "泽森" && triple.juniors.length === 2);
  ok("二、三盘同一定格流柱", triple.juniors[0].stamped.extras[0].gz === triple.juniors[1].stamped.extras[0].gz &&
    triple.juniors[0].stamped.extras[0].gz === flow.year);
  ok("妻盘正从不因定格改写", triple.juniors.filter(function (j) { return j.person.name === "妻子"; })[0]
    .stamped.zheng.pattern === H.analyzePerson(wife).zheng.pattern);
  ok("三盘亦写产生日同理", /产生日同理/.test(triple.canonZh) && /定格到/.test(triple.methodLineZh));

  if (standalone) {
    if (fails) { console.error("HECAN_FAILED", fails); process.exit(1); }
    console.log("HECAN_OK");
  } else {
    global.__hecanFails = fails;
  }
})();
