{
  "version": 3,
  "sources": ["pwa_install_core.js"],
  "sourcesContent": ["// @ts-check\n/*\n * static/js/pwa_install_core.js \u2014 when Snowdesk may ask to be installed\n * (SNOW-1513).\n *\n * The whole decision, pure: every input is passed in, so it is tested\n * without a browser (tests/js/test_pwa_install_core.js). The DOM layer,\n * static/js/pwa_install.js, reads the real environment and calls it.\n *\n * Install moments\n * ---------------\n * A prompt is asked for at a moment where installing helps the reader:\n *\n *   alerts    they switched on \"Alerts for this region\" on an iPhone in\n *             Safari, where alerts need the installed app;\n *   download  an area download finished;\n *   saved     they dropped a pin or created a trip;\n *   visit     their third visit within VISIT_WINDOW_MS;\n *   menu      they chose \"Install the app\" in the avatar or \"?\" menu.\n *\n * Every moment but ``menu`` needs ``eligible(env)``: signed in, a phone,\n * in the browser, online, not mid-task, not already asked this visit, and\n * at least the second visit (so a first visit is never interrupted).\n * ``visit`` also needs VISIT_THRESHOLD visits and no dismissal within\n * DISMISS_COOLOFF_MS. ``menu`` is the reader asking, so it needs only\n * signed in, a phone and the browser.\n *\n * Exports (frozen ``self.pwaInstallCore``):\n *   VISIT_WINDOW_MS, MIN_VISITS_FOR_MOMENT, VISIT_THRESHOLD,\n *   DISMISS_COOLOFF_MS, MOMENTS\n *   recordVisit(history, now, sessionCounted) \u2192 number[]\n *   inCoolOff(dismissedAt, now)               \u2192 boolean\n *   eligible(env)                             \u2192 boolean\n *   momentAllowed(moment, env)                \u2192 boolean\n *   pitchFor(platform)                        \u2192 'alerts' | 'app'\n */\n\n(function () {\n  'use strict';\n\n  var DAY_MS = 24 * 60 * 60 * 1000;\n  // Visits older than this no longer count towards the visit moment.\n  var VISIT_WINDOW_MS = 14 * DAY_MS;\n  // A moment is refused until this visit: the first one is never asked.\n  var MIN_VISITS_FOR_MOMENT = 2;\n  // The visit moment's own threshold: the third visit in the window.\n  var VISIT_THRESHOLD = 3;\n  // A dismissed visit banner stays away this long.\n  var DISMISS_COOLOFF_MS = 30 * DAY_MS;\n\n  var MOMENTS = Object.freeze(['alerts', 'download', 'saved', 'visit', 'menu']);\n\n  /**\n   * @typedef {Object} InstallEnv\n   * @property {boolean} signedIn           the pwa-user-id meta is non-empty\n   * @property {boolean} phone              under the 640px phone breakpoint\n   * @property {boolean} standalone         already running as the installed app\n   * @property {boolean} online             navigator.onLine and not offline mode\n   * @property {boolean} midTask            drawing, placing, a form panel, the welcome card\n   * @property {boolean} promptedThisVisit  a moment already showed this visit\n   * @property {number}  visits             visits in the window, this one included\n   * @property {number}  [dismissedAt]      ms timestamp of the last visit-banner dismissal\n   * @property {number}  [now]              ms timestamp, for the cool-off\n   */\n\n  /**\n   * The visit history after this page load: pruned to VISIT_WINDOW_MS, and\n   * with ``now`` added unless this browsing session already counted.\n   *\n   * @param {unknown} history  the stored array of ms timestamps (anything else reads as none)\n   * @param {number} now\n   * @param {boolean} sessionCounted\n   * @returns {number[]}\n   */\n  function recordVisit(history, now, sessionCounted) {\n    var list = Array.isArray(history) ? history : [];\n    /** @type {number[]} */\n    var kept = [];\n    for (var i = 0; i < list.length; i++) {\n      var at = list[i];\n      if (typeof at === 'number' && Number.isFinite(at) && at <= now && now - at < VISIT_WINDOW_MS) {\n        kept.push(at);\n      }\n    }\n    if (!sessionCounted) kept.push(now);\n    return kept;\n  }\n\n  /**\n   * True while a dismissal is under DISMISS_COOLOFF_MS old. A missing or\n   * corrupt timestamp is no dismissal, so a bad value cannot block for good.\n   *\n   * @param {unknown} dismissedAt\n   * @param {number} now\n   * @returns {boolean}\n   */\n  function inCoolOff(dismissedAt, now) {\n    if (typeof dismissedAt !== 'number' || !Number.isFinite(dismissedAt) || dismissedAt <= 0) {\n      return false;\n    }\n    return now - dismissedAt < DISMISS_COOLOFF_MS;\n  }\n\n  /**\n   * Whether the reader is somewhere they may be asked at all.\n   *\n   * @param {InstallEnv} env\n   * @returns {boolean}\n   */\n  function eligible(env) {\n    return (\n      !!env.signedIn &&\n      !!env.phone &&\n      !env.standalone &&\n      !!env.online &&\n      !env.midTask &&\n      !env.promptedThisVisit &&\n      (env.visits || 0) >= MIN_VISITS_FOR_MOMENT\n    );\n  }\n\n  /**\n   * Whether ``moment`` may show now.\n   *\n   * @param {string} moment\n   * @param {InstallEnv} env\n   * @returns {boolean}\n   */\n  function momentAllowed(moment, env) {\n    if (moment === 'menu') return !!env.signedIn && !!env.phone && !env.standalone;\n    if (MOMENTS.indexOf(moment) === -1) return false;\n    if (!eligible(env)) return false;\n    if (moment === 'visit') {\n      if ((env.visits || 0) < VISIT_THRESHOLD) return false;\n      if (inCoolOff(env.dismissedAt, env.now || 0)) return false;\n    }\n    return true;\n  }\n\n  /**\n   * What the banner leads with: alerts on iPhone, where they need the app,\n   * and the app itself everywhere else.\n   *\n   * @param {string} platform  'ios' | 'android' | 'desktop'\n   * @returns {'alerts' | 'app'}\n   */\n  function pitchFor(platform) {\n    return platform === 'ios' ? 'alerts' : 'app';\n  }\n\n  self.pwaInstallCore = Object.freeze({\n    VISIT_WINDOW_MS: VISIT_WINDOW_MS,\n    MIN_VISITS_FOR_MOMENT: MIN_VISITS_FOR_MOMENT,\n    VISIT_THRESHOLD: VISIT_THRESHOLD,\n    DISMISS_COOLOFF_MS: DISMISS_COOLOFF_MS,\n    MOMENTS: MOMENTS,\n    recordVisit: recordVisit,\n    inCoolOff: inCoolOff,\n    eligible: eligible,\n    momentAllowed: momentAllowed,\n    pitchFor: pitchFor,\n  });\n})();\n"],
  "mappings": "CAqCC,UAAY,CACX,aAEA,IAAIA,EAAS,MAETC,EAAkB,GAAKD,EAEvBE,EAAwB,EAExBC,EAAkB,EAElBC,EAAqB,GAAKJ,EAE1BK,EAAU,OAAO,OAAO,CAAC,SAAU,WAAY,QAAS,QAAS,MAAM,CAAC,EAwB5E,SAASC,EAAYC,EAASC,EAAKC,EAAgB,CAIjD,QAHIC,EAAO,MAAM,QAAQH,CAAO,EAAIA,EAAU,CAAC,EAE3CI,EAAO,CAAC,EACHC,EAAI,EAAGA,EAAIF,EAAK,OAAQE,IAAK,CACpC,IAAIC,EAAKH,EAAKE,CAAC,EACX,OAAOC,GAAO,UAAY,OAAO,SAASA,CAAE,GAAKA,GAAML,GAAOA,EAAMK,EAAKZ,GAC3EU,EAAK,KAAKE,CAAE,CAEhB,CACA,OAAKJ,GAAgBE,EAAK,KAAKH,CAAG,EAC3BG,CACT,CAUA,SAASG,EAAUC,EAAaP,EAAK,CACnC,OAAI,OAAOO,GAAgB,UAAY,CAAC,OAAO,SAASA,CAAW,GAAKA,GAAe,EAC9E,GAEFP,EAAMO,EAAcX,CAC7B,CAQA,SAASY,EAASC,EAAK,CACrB,MACE,CAAC,CAACA,EAAI,UACN,CAAC,CAACA,EAAI,OACN,CAACA,EAAI,YACL,CAAC,CAACA,EAAI,QACN,CAACA,EAAI,SACL,CAACA,EAAI,oBACJA,EAAI,QAAU,IAAMf,CAEzB,CASA,SAASgB,EAAcC,EAAQF,EAAK,CAClC,OAAIE,IAAW,OAAe,CAAC,CAACF,EAAI,UAAY,CAAC,CAACA,EAAI,OAAS,CAACA,EAAI,WAChE,EAAAZ,EAAQ,QAAQc,CAAM,IAAM,IAC5B,CAACH,EAASC,CAAG,GACbE,IAAW,WACRF,EAAI,QAAU,GAAKd,GACpBW,EAAUG,EAAI,YAAaA,EAAI,KAAO,CAAC,GAG/C,CASA,SAASG,EAASC,EAAU,CAC1B,OAAOA,IAAa,MAAQ,SAAW,KACzC,CAEA,KAAK,eAAiB,OAAO,OAAO,CAClC,gBAAiBpB,EACjB,sBAAuBC,EACvB,gBAAiBC,EACjB,mBAAoBC,EACpB,QAASC,EACT,YAAaC,EACb,UAAWQ,EACX,SAAUE,EACV,cAAeE,EACf,SAAUE,CACZ,CAAC,CACH,GAAG",
  "names": ["DAY_MS", "VISIT_WINDOW_MS", "MIN_VISITS_FOR_MOMENT", "VISIT_THRESHOLD", "DISMISS_COOLOFF_MS", "MOMENTS", "recordVisit", "history", "now", "sessionCounted", "list", "kept", "i", "at", "inCoolOff", "dismissedAt", "eligible", "env", "momentAllowed", "moment", "pitchFor", "platform"]
}
