{
  "version": 3,
  "sources": ["help_filter.js"],
  "sourcesContent": ["// @ts-check\n/**\n * static/js/help_filter.js \u2014 the filter box at the top of /help/ (SNOW-743).\n *\n * /help/ holds twenty collapsed topics under four eyebrow headings, and a\n * reader who arrives knowing the word for what they want (\"gpx\",\n * \"offline\", \"slope\") should not have to open them one at a time. Typing\n * here hides every topic whose text does not match, opens the ones that\n * do, marks the matched words, and hides a heading left with no topic.\n * Clearing the box puts the page back exactly as it was before the first\n * keystroke, including a topic a #help-topic-\u2026 link had opened.\n *\n * What it matches:\n *   - each topic's own words, its title and body, folded to lower case\n *     with accents stripped (so TELEPHERIQUE finds t\u00E9l\u00E9ph\u00E9rique);\n *   - never a topic's illustration ([data-testid$=\"-illustration\"]): those\n *     are live mock components whose sample copy would make false hits;\n *   - a short synonym map, so a reader's word finds the topic that uses\n *     ours: when the query's own word matches nothing, a query naming a\n *     synonym key searches for its values instead.\n *\n * This is the deliberate alternative to a semantic search: the corpus is\n * one page, nothing here fetches, and the page keeps working offline.\n *\n * The box is rendered ``hidden`` and only this script reveals it, so with\n * JavaScript off there is no control that does nothing. Highlighting\n * splits text nodes and wraps runs in <mark>; it never writes innerHTML.\n * Every string comes from #help-filter-strings-template via\n * window.pwaStrings, with the English literal as the fallback.\n */\n\n(function helpFilterInit() {\n  'use strict';\n\n  /**\n   * A reader's word \u2192 the words the topics use for it. A key matches a\n   * query that equals it or that it starts with (three letters or more),\n   * so \"favor\" reaches \"favorite\" and so the topic that says \"favourite\".\n   *\n   * @type {Readonly<Record<string, readonly string[]>>}\n   */\n  const SYNONYMS = Object.freeze({\n    gpx: ['route'],\n    track: ['route'],\n    tour: ['route'],\n    cache: ['offline', 'download'],\n    storage: ['offline', 'download'],\n    basemap: ['layer'],\n    swisstopo: ['layer'],\n    steep: ['slope'],\n    gradient: ['slope'],\n    incline: ['slope'],\n    favorite: ['favourite'],\n    pin: ['favourite'],\n    star: ['favourite'],\n    report: ['observation'],\n    login: ['account', 'sign in'],\n    password: ['account', 'sign in'],\n    email: ['account'],\n    app: ['install'],\n    pwa: ['install'],\n    danger: ['bulletin'],\n    risk: ['bulletin'],\n    forecast: ['weather'],\n    temperature: ['weather'],\n    wind: ['weather'],\n    season: ['calendar'],\n    history: ['calendar', 'timeline'],\n    date: ['timeline'],\n  });\n\n  const MARK_CLASS = 'rounded-sm bg-status-warning-bg text-text-1';\n  const ILLUSTRATION = '[data-testid$=\"-illustration\"]';\n\n  /**\n   * Fold a string for matching: lower case, accents stripped. Folded one\n   * character at a time so ``index[i]`` names the source character the\n   * i-th folded one came from, which is how a match is mapped back onto\n   * the text node to mark it.\n   *\n   * @param {string} text\n   * @returns {{ folded: string, index: number[] }}\n   */\n  function fold(text) {\n    let folded = '';\n    /** @type {number[]} */\n    const index = [];\n    for (let i = 0; i < text.length; i += 1) {\n      const piece = text[i].normalize('NFD').replace(/[\u0300-\u036F]/g, '').toLowerCase();\n      for (let j = 0; j < piece.length; j += 1) {\n        folded += piece[j];\n        index.push(i);\n      }\n    }\n    return { folded, index };\n  }\n\n  /**\n   * The folded query, and the terms it widens to: itself plus the\n   * synonyms of every key it names. The widened set is only searched when\n   * the query's own word finds nothing, so \"gpx\" lands on the one topic\n   * that says GPX rather than on every topic that mentions a route.\n   *\n   * @param {string} query\n   * @returns {{ literal: string[], widened: string[] }}\n   */\n  function termsFor(query) {\n    const q = fold(query).folded.replace(/\\s+/g, ' ').trim();\n    if (!q) return { literal: [], widened: [] };\n    const terms = new Set([q]);\n    Object.keys(SYNONYMS).forEach((key) => {\n      if (key === q || (q.length >= 3 && key.startsWith(q))) {\n        SYNONYMS[key].forEach((word) => terms.add(word));\n      }\n    });\n    return { literal: [q], widened: Array.from(terms) };\n  }\n\n  /**\n   * The text nodes of a topic that count: everything but its illustration.\n   *\n   * @param {Element} panel\n   * @returns {Text[]}\n   */\n  function textNodes(panel) {\n    /** @type {Text[]} */\n    const nodes = [];\n    const walker = document.createTreeWalker(panel, NodeFilter.SHOW_TEXT, {\n      acceptNode(node) {\n        const parent = node.parentElement;\n        if (!parent || parent.closest(`${ILLUSTRATION}, script, template, style`)) {\n          return NodeFilter.FILTER_REJECT;\n        }\n        return NodeFilter.FILTER_ACCEPT;\n      },\n    });\n    for (let node = walker.nextNode(); node; node = walker.nextNode()) {\n      nodes.push(/** @type {Text} */ (node));\n    }\n    return nodes;\n  }\n\n  /**\n   * The source ranges in a string that any term matches, merged.\n   *\n   * @param {string} text\n   * @param {string[]} terms\n   * @returns {Array<[number, number]>}\n   */\n  function matchRanges(text, terms) {\n    const { folded, index } = fold(text);\n    /** @type {Array<[number, number]>} */\n    const ranges = [];\n    terms.forEach((term) => {\n      let at = folded.indexOf(term);\n      while (at !== -1) {\n        ranges.push([index[at], index[at + term.length - 1] + 1]);\n        at = folded.indexOf(term, at + term.length);\n      }\n    });\n    ranges.sort((a, b) => a[0] - b[0]);\n    /** @type {Array<[number, number]>} */\n    const merged = [];\n    ranges.forEach((range) => {\n      const last = merged[merged.length - 1];\n      if (last && range[0] <= last[1]) last[1] = Math.max(last[1], range[1]);\n      else merged.push([range[0], range[1]]);\n    });\n    return merged;\n  }\n\n  /**\n   * Wrap each matched run of a text node in a <mark>, by splitting it.\n   *\n   * @param {Text} node\n   * @param {Array<[number, number]>} ranges Source ranges, ascending.\n   */\n  function markNode(node, ranges) {\n    let current = node;\n    let offset = 0;\n    ranges.forEach(([start, end]) => {\n      const run = current.splitText(start - offset);\n      const rest = run.splitText(end - start);\n      const mark = document.createElement('mark');\n      mark.className = MARK_CLASS;\n      mark.setAttribute('data-help-filter-mark', '');\n      run.replaceWith(mark);\n      mark.appendChild(run);\n      current = rest;\n      offset = end;\n    });\n  }\n\n  /**\n   * Remove every <mark> this module added, merging the text back.\n   *\n   * @param {ParentNode} root\n   */\n  function unmark(root) {\n    root.querySelectorAll('mark[data-help-filter-mark]').forEach((mark) => {\n      const parent = mark.parentNode;\n      mark.replaceWith(...Array.from(mark.childNodes));\n      if (parent) parent.normalize();\n    });\n  }\n\n  /** Bind the box on /help/, if this page has one. */\n  function init() {\n    const box = /** @type {HTMLElement | null} */ (document.querySelector('[data-help-filter]'));\n    const input = box && box.querySelector('[data-help-filter-input]');\n    const main = box && box.closest('main');\n    if (!box || !(input instanceof HTMLInputElement) || !main) return;\n    const status = box.querySelector('[data-help-filter-status]');\n\n    // i18n_strings.js is loaded (deferred, earlier in the page) by\n    // public/base.html, so it has run before this deferred module.\n    /** @type {Record<string, string>} */\n    const STRINGS = window.pwaStrings.read('help-filter-strings-template', {\n      'no-match': 'No topic matches that. Try a different word.',\n      'one-match': '1 topic matches.',\n      'some-match': '%(count)s topics match.',\n    });\n    /**\n     * @param {string} template\n     * @param {number} count\n     * @returns {string}\n     */\n    const withCount = (template, count) => window.pwaStrings.interpolate(template, { count });\n\n    /** Topics and headings in document order; a heading owns what follows it. */\n    const items = Array.from(\n      main.querySelectorAll('[data-hook=\"help-section\"], details.snowdesk-panel[id^=\"help-topic-\"]'),\n    );\n    /** @type {Map<Element, HTMLDetailsElement[]>} */\n    const sections = new Map();\n    /** @type {HTMLDetailsElement[]} */\n    const panels = [];\n    /** @type {Element | null} */\n    let heading = null;\n    items.forEach((item) => {\n      if (item instanceof HTMLDetailsElement) {\n        panels.push(item);\n        if (heading) sections.get(heading)?.push(item);\n      } else {\n        heading = item;\n        sections.set(item, []);\n      }\n    });\n\n    /** Which topics were open before the first keystroke; null when unfiltered. */\n    /** @type {Map<HTMLDetailsElement, boolean> | null} */\n    let before = null;\n\n    /** @param {string} text */\n    const say = (text) => {\n      if (status) status.textContent = text;\n    };\n\n    const apply = () => {\n      unmark(main);\n      const { literal, widened } = termsFor(input.value);\n      if (!literal.length) {\n        if (before) {\n          before.forEach((open, panel) => { panel.open = open; });\n          before = null;\n        }\n        panels.forEach((panel) => { panel.hidden = false; });\n        sections.forEach((_, item) => { /** @type {HTMLElement} */ (item).hidden = false; });\n        say('');\n        return;\n      }\n      if (!before) before = new Map(panels.map((panel) => [panel, panel.open]));\n\n      /**\n       * Each topic's text nodes that the terms match, with their ranges.\n       *\n       * @param {string[]} terms\n       * @returns {Array<Array<[Text, Array<[number, number]>]>>}\n       */\n      const hitsFor = (terms) => panels.map((panel) => textNodes(panel)\n        .map((node) => /** @type {[Text, Array<[number, number]>]} */ (\n          [node, matchRanges(node.data, terms)]\n        ))\n        .filter(([, ranges]) => ranges.length));\n\n      let hits = hitsFor(literal);\n      if (!hits.some((found) => found.length) && widened.length > literal.length) {\n        hits = hitsFor(widened);\n      }\n\n      let shown = 0;\n      panels.forEach((panel, i) => {\n        const found = hits[i];\n        panel.hidden = !found.length;\n        if (!found.length) return;\n        shown += 1;\n        panel.open = true;\n        found.forEach(([node, ranges]) => markNode(node, ranges));\n      });\n      sections.forEach((owned, item) => {\n        /** @type {HTMLElement} */ (item).hidden = !owned.some((panel) => !panel.hidden);\n      });\n\n      if (!shown) say(STRINGS['no-match']);\n      else if (shown === 1) say(STRINGS['one-match']);\n      else say(withCount(STRINGS['some-match'], shown));\n    };\n\n    input.addEventListener('input', apply);\n    box.hidden = false;\n  }\n\n  init();\n}());\n"],
  "mappings": "CA+BC,UAA0B,CACzB,aASA,MAAMA,EAAW,OAAO,OAAO,CAC7B,IAAK,CAAC,OAAO,EACb,MAAO,CAAC,OAAO,EACf,KAAM,CAAC,OAAO,EACd,MAAO,CAAC,UAAW,UAAU,EAC7B,QAAS,CAAC,UAAW,UAAU,EAC/B,QAAS,CAAC,OAAO,EACjB,UAAW,CAAC,OAAO,EACnB,MAAO,CAAC,OAAO,EACf,SAAU,CAAC,OAAO,EAClB,QAAS,CAAC,OAAO,EACjB,SAAU,CAAC,WAAW,EACtB,IAAK,CAAC,WAAW,EACjB,KAAM,CAAC,WAAW,EAClB,OAAQ,CAAC,aAAa,EACtB,MAAO,CAAC,UAAW,SAAS,EAC5B,SAAU,CAAC,UAAW,SAAS,EAC/B,MAAO,CAAC,SAAS,EACjB,IAAK,CAAC,SAAS,EACf,IAAK,CAAC,SAAS,EACf,OAAQ,CAAC,UAAU,EACnB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U,CACY,CAEhD,EAEAH,EAAM,iBAAiB,QAASc,CAAK,EACrCf,EAAI,OAAS,EACf,CAEAD,EAAK,CACP,GAAE",
  "names": ["SYNONYMS", "MARK_CLASS", "ILLUSTRATION", "fold", "text", "folded", "index", "i", "piece", "j", "termsFor", "query", "q", "terms", "key", "word", "textNodes", "panel", "nodes", "walker", "node", "parent", "matchRanges", "ranges", "term", "at", "a", "b", "merged", "range", "last", "markNode", "current", "offset", "start", "end", "run", "rest", "mark", "unmark", "root", "init", "box", "input", "main", "status", "STRINGS", "withCount", "template", "count", "items", "sections", "panels", "heading", "item", "before", "say", "apply", "literal", "widened", "open", "_", "hitsFor", "hits", "found", "shown", "owned"]
}
