// Multi-level knowledge tree: mirrors docs/wiki Markdown ## structure + source PDFs.
// GitHub heading anchors follow github.com slug rules (ASCII headings).

(function wikiTreeSetup() {
  function repoBlob(repoRelativePath) {
    var parts = repoRelativePath.split("/");
    return "https://github.com/crewio-ai/ech-survey/blob/main/" + parts.map(encodeURIComponent).join("/");
  }

  function md(filePath, slug) {
    var b = repoBlob(filePath);
    return slug ? b + "#" + slug : b;
  }

  /** Section row: in-doc anchor (English slugs match GitHub-rendered README preview). */
  function sec(id, title, titleTc, filePath, anchorSlug) {
    return {
      id: id,
      title: title,
      titleTc: titleTc || "",
      href: md(filePath, anchorSlug),
      kind: "section",
    };
  }

  /** Section heading shown in tree only (no anchor — e.g. Chinese heading IDs vary on GitHub). */
  function leaf(id, title, titleTc) {
    return { id: id, title: title, titleTc: titleTc || "", kind: "section" };
  }

  window.repoBlobUrl = repoBlob;

  window.WIKI_COVERAGE_STATS = {
    wikiMarkdownArticles: 9,
    registeredSourcePdfs: 7,
    questionnaireSourcePdfs: 3,
    vasRuleGuidePdfs: 4,
    bilingualSurveyMarkdownTables: 2,
    specialistRuleWikiPages: 2,
    taglineEn:
      "Multi-level tree: each wiki page expands to its Markdown sections (##); questionnaires expand to numbered form sections; sources list registered PDFs. Same files as GitHub docs/wiki.",
    taglineTc:
      "多層知識樹：每個 Wiki 頁拆成 Markdown 章節（##）；問卷再拆編號段落；來源表列出已登記 PDF。與 GitHub docs/wiki 同源。",
  };

  var WIKI = "docs/wiki";
  var SUR = "docs/wiki/surveys";
  var RULES = "docs/wiki/rules";

  var README_SECTIONS = [
    sec("rm-start", "Start Here", "開始", WIKI + "/README.md", "start-here"),
    sec("rm-why", "Why Markdown", "為何使用 Markdown", WIKI + "/README.md", "why-markdown"),
    sec("rm-own", "Ownership Model", "擁有權模型", WIKI + "/README.md", "ownership-model"),
    sec("rm-scope", "Current Scope", "現有範圍", WIKI + "/README.md", "current-scope"),
  ];

  var METHODOLOGY_SECTIONS = [
    sec("met-src", "Source Responsibility", "來源責任", WIKI + "/methodology.md", "source-responsibility"),
    sec("met-shape", "Rule Shape", "規則結構", WIKI + "/methodology.md", "rule-shape"),
    sec("met-flow", "Decision Flow", "決策流程", WIKI + "/methodology.md", "decision-flow"),
    sec("met-pri", "Priority Rules", "優先規則", WIKI + "/methodology.md", "priority-rules"),
    sec("met-cov", "Questionnaire Coverage Model", "問卷覆蓋模型", WIKI + "/methodology.md", "questionnaire-coverage-model"),
    sec("met-skip", "Questionnaire Journey And Skip Logic", "問卷旅程與跳題", WIKI + "/methodology.md", "questionnaire-journey-and-skip-logic"),
    sec("met-prec", "Data Source Precedence", "數據優先次序", WIKI + "/methodology.md", "data-source-precedence"),
    sec("met-chg", "Change Control", "變更控制", WIKI + "/methodology.md", "change-control"),
    sec("met-next", "Recommended Next Step", "建議下一步", WIKI + "/methodology.md", "recommended-next-step"),
  ];

  var QMAP_SECTIONS = [
    sec("qm-sm", "Section Map", "問卷段落對照表", WIKI + "/questionnaire-map.md", "section-map"),
    sec("qm-sig", "Rule-Relevant Questionnaire Signals", "規則相關訊號", WIKI + "/questionnaire-map.md", "rule-relevant-questionnaire-signals"),
    sec("qm-branch", "Branching / Skip Logic", "分支／跳題邏輯", WIKI + "/questionnaire-map.md", "branching--skip-logic"),
    sec("qm-gap", "Gaps To Resolve", "待處理差異", WIKI + "/questionnaire-map.md", "gaps-to-resolve"),
    sec("qm-rec", "Recommendation", "建議", WIKI + "/questionnaire-map.md", "recommendation"),
  ];

  var SRC_PDFS = [
    {
      id: "src-q-eng",
      title: "SRC-Q-ENG-001 · English questionnaire PDF",
      titleTc: "英文問卷原件",
      href: repoBlob("docs/source-documents/questionnaires/MH Form_Eng_MM+ (1).pdf"),
      kind: "asset",
    },
    {
      id: "src-q-zh",
      title: "SRC-Q-ZH-001 · Chinese questionnaire PDF",
      titleTc: "中文問卷原件",
      href: repoBlob("docs/source-documents/questionnaires/MH Form_中文MM+ (1).pdf"),
      kind: "asset",
    },
    {
      id: "src-q-zh-v3",
      title: "SRC-Q-ZH-003 · Chinese v3 + new specialist triggers",
      titleTc: "中文 v3・新增觸發題",
      href: repoBlob("docs/source-documents/questionnaires/MH_Form_中文_v3_with_new_questions.pdf"),
      kind: "asset",
    },
    {
      id: "src-r-og-12",
      title: "SRC-R-OG-001 · Prima O&G rules v1.2",
      titleTc: "Prima O&G 規則 v1.2",
      href: repoBlob(
        "docs/source-documents/vas-action-rules/ReHealth_PrimeCare_v1.2_with_patient_explanation (1).pdf"
      ),
      kind: "asset",
    },
    {
      id: "src-r-ped-12",
      title: "SRC-R-PED-001 · Paediatrics rules v1.2",
      titleTc: "兒科規則 v1.2",
      href: repoBlob(
        "docs/source-documents/vas-action-rules/ReHealth_PrimeCare_v1.2_with_patient_explanation.pdf"
      ),
      kind: "asset",
    },
    {
      id: "src-r-og-13",
      title: "SRC-R-OG-003 · O&G rules v1.3 · Package fees",
      titleTc: "婦科規則 v1.3・套餐參考價",
      href: repoBlob(
        "docs/source-documents/vas-action-rules/ReHealth_PrimeCare_v1.3_with_package_fee(OG).pdf"
      ),
      kind: "asset",
    },
    {
      id: "src-r-ped-13",
      title: "SRC-R-PED-003 · Paediatrics rules v1.3 · Package fees",
      titleTc: "兒科規則 v1.3・套餐參考價",
      href: repoBlob(
        "docs/source-documents/vas-action-rules/ReHealth_PrimeCare_v1.3_with_package_fee(Child).pdf"
      ),
      kind: "asset",
    },
  ];

  var SOURCE_REGISTER_SECTIONS = [
    {
      id: "sr-pdfs",
      title: "§ Source Documents · registered PDFs",
      titleTc: "來源文件（登記表）",
      kind: "group",
      children: SRC_PDFS,
    },
    sec("sr-vas", "§ VAS Action Sources", "增值服務動作來源", WIKI + "/source-register.md", "vas-action-sources"),
    sec("sr-qual", "§ Source Quality Notes", "來源質素說明", WIKI + "/source-register.md", "source-quality-notes"),
    sec("sr-leg", "§ Status Legend", "狀態標記", WIKI + "/source-register.md", "status-legend"),
    sec("sr-maint", "§ Maintenance Rule", "維護規則", WIKI + "/source-register.md", "maintenance-rule"),
  ];

  var SURVEY_EN_SECTIONS = [
    sec("en-purp", "Purpose Of Collection Of Personal Data", "收集個人資料目的", SUR + "/pre-exam-health-assessment-en.md", "purpose-of-collection-of-personal-data"),
    sec("en-1", "1. Personal Particulars", "1. 個人資料", SUR + "/pre-exam-health-assessment-en.md", "1-personal-particulars"),
    sec("en-2", "2. Pre-Examination Clinical Information", "2. 體檢前臨床資料", SUR + "/pre-exam-health-assessment-en.md", "2-pre-examination-clinical-information"),
    sec("en-3", "3. Recent Health Status - Past 12 Months", "3. 最近健康狀況", SUR + "/pre-exam-health-assessment-en.md", "3-recent-health-status---past-12-months"),
    sec("en-4", "4. Past Medical History", "4. 過往病歷", SUR + "/pre-exam-health-assessment-en.md", "4-past-medical-history"),
    sec("en-5", "5. Family Medical History", "5. 家族病歷", SUR + "/pre-exam-health-assessment-en.md", "5-family-medical-history"),
    sec("en-6", "6. Previous Ultrasound Examination Records", "6. 超聲波紀錄", SUR + "/pre-exam-health-assessment-en.md", "6-previous-ultrasound-examination-records"),
    sec("en-7", "7. Important Medical Information", "7. 重要醫療資訊", SUR + "/pre-exam-health-assessment-en.md", "7-important-medical-information"),
    sec("en-8", "8. Pre-Examination Confirmation And Consent", "8. 體檢前確認及同意", SUR + "/pre-exam-health-assessment-en.md", "8-pre-examination-confirmation-and-consent"),
    sec("en-9", "9. Client Declaration And Consent", "9. 客戶聲明及同意", SUR + "/pre-exam-health-assessment-en.md", "9-client-declaration-and-consent"),
  ];

  var SURVEY_ZH_SECTIONS = [
    leaf("zh-purp", "收集個人資料目的", "Purpose"),
    leaf("zh-1", "1. 個人資料", "1. Personal"),
    leaf("zh-2", "2. 體檢前臨床資料", "2. Clinical"),
    leaf("zh-3", "3. 最近健康狀況 - 過去 12 個月", "3. Recent health"),
    leaf("zh-4", "4. 過往病歷", "4. Past history"),
    leaf("zh-5", "5. 家族病歷", "5. Family"),
    leaf("zh-6", "6. 過往超聲波檢查紀錄", "6. Ultrasound"),
    leaf("zh-7", "7. 重要醫療資訊", "7. Important info"),
    leaf("zh-8", "8. 體檢前確認及同意", "8. Consent"),
    leaf("zh-9", "9. 客戶聲明及同意", "9. Declaration"),
  ];

  var OG_RULE_SECTIONS = [
    sec("og-grp", "§ Rule Groups", "規則分組", RULES + "/og.md", "rule-groups"),
    sec("og-cat", "§ Rule Catalogue", "規則目錄（表格）", RULES + "/og.md", "rule-catalogue"),
    sec("og-pex", "§ Patient Explanation Text", "病人說明原文", RULES + "/og.md", "patient-explanation-text"),
    sec("og-ph", "§ Patient Explanation Handling", "病人說明使用方式", RULES + "/og.md", "patient-explanation-handling"),
    sec("og-impl", "§ Implementation Notes", "實施備註", RULES + "/og.md", "implementation-notes"),
  ];

  var PED_RULE_SECTIONS = [
    sec("ped-grp", "§ Rule Groups", "規則分組", RULES + "/paediatrics.md", "rule-groups"),
    sec("ped-cat", "§ Rule Catalogue", "規則目錄（表格）", RULES + "/paediatrics.md", "rule-catalogue"),
    sec("ped-impl", "§ Implementation Notes", "實施備註", RULES + "/paediatrics.md", "implementation-notes"),
    sec("ped-ph", "§ Patient Explanation Handling", "家長／監護人說明", RULES + "/paediatrics.md", "patient-explanation-handling"),
  ];

  window.WIKI_KNOWLEDGE_TREE = {
    id: "wiki-root",
    title: "ECH Survey Wiki",
    titleTc: "ECH Survey 知識庫",
    blurb:
      "Nested Markdown sections mirror each file’s ## headings; PDFs are registered assets. Follow links to GitHub for full tables and text.",
    blurbTc:
      "章節層級對應各 Markdown 檔的 ## 標題；PDF 為登記資產。連結會於 GitHub 開啟全文表格與原文。",
    href: repoBlob(WIKI + "/README.md"),
    kind: "root",
    children: [
      {
        id: "readme-sections",
        title: "README · Hub sections",
        titleTc: "總覽（README 章節）",
        href: repoBlob(WIKI + "/README.md"),
        kind: "doc",
        children: README_SECTIONS,
      },
      {
        id: "methodology-flow",
        title: "Rule methodology · Decision flow",
        titleTc: "規則方法論・決策流程",
        blurb:
          "Separates inputs, trigger logic, answer states, skip logic, and change control.",
        blurbTc: "區分輸入來源、觸發邏輯、答案狀態、跳題與變更控制。",
        href: repoBlob(WIKI + "/methodology.md"),
        kind: "doc",
        children: METHODOLOGY_SECTIONS,
      },
      {
        id: "questionnaire-map",
        title: "Questionnaire map · Branching",
        titleTc: "問卷對照・分支邏輯",
        blurb: "Nine survey domains, signal IDs, branching table, gaps.",
        blurbTc: "九個問卷段落、訊號 ID、分支表、待對齊項目。",
        href: repoBlob(WIKI + "/questionnaire-map.md"),
        kind: "doc",
        children: QMAP_SECTIONS,
      },
      {
        id: "source-register",
        title: "Source register · PDF index",
        titleTc: "來源登記・PDF 索引",
        blurb: "Registered sources: questionnaire PDFs vs VAS rule PDFs.",
        blurbTc: "登記問卷原件與規則原件；區分輸入與增值動作來源。",
        href: repoBlob(WIKI + "/source-register.md"),
        kind: "doc",
        children: SOURCE_REGISTER_SECTIONS,
      },
      {
        id: "surveys-md",
        title: "Survey Markdown · Field IDs",
        titleTc: "問卷 Markdown・Field ID",
        blurb: "Survey folder README plus EN/ZH forms broken into numbered sections.",
        blurbTc: "問卷目錄說明及中英表格按編號段落展開。",
        href: repoBlob(SUR + "/README.md"),
        kind: "doc",
        children: [
          sec("sv-conv", "§ Survey index · Converted Surveys", "問卷索引・已轉換列表", SUR + "/README.md", "converted-surveys"),
          {
            id: "survey-en",
            title: "Pre-Examination Health Assessment · EN",
            titleTc: "體檢前健康評估表・英文",
            href: repoBlob(SUR + "/pre-exam-health-assessment-en.md"),
            kind: "doc",
            children: SURVEY_EN_SECTIONS,
          },
          {
            id: "survey-zh",
            title: "體檢前健康評估表 · 中文",
            titleTc: "中文問卷對照稿",
            href: repoBlob(SUR + "/pre-exam-health-assessment-zh.md"),
            kind: "doc",
            children: SURVEY_ZH_SECTIONS,
          },
          sec("sv-map", "§ Mapping Rule", "Field ID 對應規則", SUR + "/README.md", "mapping-rule"),
          sec("sv-q", "§ Source Quality", "來源質素", SUR + "/README.md", "source-quality"),
        ],
      },
      {
        id: "rules-md",
        title: "Specialist rule catalogues · Markdown",
        titleTc: "專科規則目錄・Markdown",
        blurb: "O&G and paediatrics: rule tables + patient explanation handling.",
        blurbTc: "婦科／兒科規則表與病人說明處理。",
        kind: "group",
        children: [
          {
            id: "rules-og",
            title: "Prima O&G · Rules & explanations",
            titleTc: "Prima 婦科・規則與病人說明",
            href: repoBlob(RULES + "/og.md"),
            kind: "doc",
            children: OG_RULE_SECTIONS,
          },
          {
            id: "rules-ped",
            title: "PrimeCare Paediatrics · Rules & explanations",
            titleTc: "PrimeCare 兒科・規則與病人說明",
            href: repoBlob(RULES + "/paediatrics.md"),
            kind: "doc",
            children: PED_RULE_SECTIONS,
          },
        ],
      },
    ],
  };
})();
