// # @hook province-atlas-config 20260817
// Provincial hydrology atlas configuration for basin hydrology and design flood workflows.
(() => {
  "use strict";

  const STORAGE_KEY = "htzd:flood-region-code";
  const COMMON_PARAMETER_GROUPS = [
    "本省暴雨/洪水图集或水文手册",
    "设计点雨量、变差系数 Cv、偏态系数 Cs/Cv",
    "点面折减、暴雨分区、产流/入渗分区",
    "参证站资料、地区经验参数、频率曲线参数",
  ];

  const PENDING_METHOD_GROUPS = [
    {
      title: "待接入本省资料后启用",
      methods: ["设计暴雨", "产汇流", "经验公式", "参证站比拟", "频率分析"],
    },
  ];

  const REGIONS = [
    ["BJ", "北京市", "直辖市"],
    ["TJ", "天津市", "直辖市"],
    ["HE", "河北省", "省"],
    ["SX", "山西省", "省"],
    ["NM", "内蒙古自治区", "自治区"],
    ["LN", "辽宁省", "省"],
    ["JL", "吉林省", "省"],
    ["HLJ", "黑龙江省", "省"],
    ["SH", "上海市", "直辖市"],
    ["JS", "江苏省", "省"],
    ["ZJ", "浙江省", "省"],
    ["AH", "安徽省", "省"],
    ["FJ", "福建省", "省"],
    ["JX", "江西省", "省"],
    ["SD", "山东省", "省"],
    ["HA", "河南省", "省"],
    ["HB", "湖北省", "省"],
    ["HN", "湖南省", "省"],
    ["GD", "广东省", "省"],
    ["GX", "广西壮族自治区", "自治区"],
    ["HI", "海南省", "省"],
    ["CQ", "重庆市", "直辖市"],
    ["SC", "四川省", "省"],
    ["GZ", "贵州省", "省"],
    ["YN", "云南省", "省"],
    ["XZ", "西藏自治区", "自治区"],
    ["SN", "陕西省", "省"],
    ["GS", "甘肃省", "省"],
    ["QH", "青海省", "省"],
    ["NX", "宁夏回族自治区", "自治区"],
    ["XJ", "新疆维吾尔自治区", "自治区"],
  ];

  function pendingConfig(code, name, type) {
    return {
      code,
      name,
      type,
      statusKind: "pending",
      statusLabel: "本省资料待接入",
      atlasSources: [`${name}暴雨洪水图集/水文计算图集（待接入）`, `${name}水文手册或地方审定参数（待接入）`],
      methodGroups: PENDING_METHOD_GROUPS,
      parameterGroups: COMMON_PARAMETER_GROUPS,
      notices: [
        `当前尚未接入${name}水文计算图集、等值线和地方经验参数，不能套用甘肃或其他省份参数。`,
        "请先补充本省图集、规范条文、参数表或审定成果；缺资料时软件只显示清单，不自动编造结果。",
      ],
      canCalculate: false,
    };
  }

  const configs = {};
  for (const [code, name, type] of REGIONS) {
    configs[code] = pendingConfig(code, name, type);
  }

  configs.GS = {
    code: "GS",
    name: "甘肃省",
    type: "省",
    statusKind: "configured",
    statusLabel: "甘肃资料体系已启用",
    atlasSources: [
      "《甘肃省暴雨洪水图集》",
      "《甘肃省中小河流暴雨洪水研究报告》",
      "《水文计算图表集》",
      "用户已提供的甘肃点雨量、Cv 等值线图集资料（继续整理入库）",
    ],
    methodGroups: [
      {
        title: "设计暴雨与产汇流",
        methods: ["铁一两所法", "地区经验公式", "瞬时单位线法", "推理公式法", "中小河流经验公式法"],
      },
      {
        title: "参证站与频率分析",
        methods: ["水文面积比拟法", "双对数内插法", "水文频率适线计算"],
      },
      {
        title: "成果整理与衔接",
        methods: ["洪水过程线合成", "水库洪峰洪量", "设计洪水成果对比", "下游水面线衔接"],
      },
    ],
    parameterGroups: [
      "暴雨分区、雨型分区、产流/入渗分区",
      "1h、6h、24h 点雨量均值，Cv，Cs/Cv",
      "点面折减系数、频率系数、形状修正系数",
      "流域面积、主河道长度、比降、参证站面积与频率资料",
    ],
    notices: [
      "H 类雨量输入必须明确为点雨量均值或审定设计雨量；面雨量由点雨量乘 Kp、点面折减和相关修正参数得到。",
      "不是所有洪水计算方法都需要分区；只有公式明确需要雨型、入渗或地区参数时才显示并参与计算。",
      "等值线自动读数仍需依托已入库图集数据；缺少图集读数时不得自动编造点雨量、Cv 或地区参数。",
    ],
    canCalculate: true,
  };

  function options() {
    return REGIONS.map(([code, name, type]) => ({ code, name, type, statusKind: configs[code].statusKind }));
  }

  function getConfig(code) {
    return configs[code] || configs.GS;
  }

  function getSelectedCode() {
    return localStorage.getItem(STORAGE_KEY) || "GS";
  }

  function setSelectedCode(code) {
    const normalized = configs[code] ? code : "GS";
    localStorage.setItem(STORAGE_KEY, normalized);
    return normalized;
  }

  window.HYDROLOGY_REGION_STORAGE_KEY = STORAGE_KEY;
  window.HYDROLOGY_REGION_CONFIGS = configs;
  window.getHydrologyRegionOptions = options;
  window.getHydrologyRegionConfig = getConfig;
  window.getSelectedHydrologyRegionCode = getSelectedCode;
  window.setSelectedHydrologyRegionCode = setSelectedCode;
})();
