{"version":3,"file":"createMemoryHistory.js","names":[],"sources":["esm/fork/createMemoryHistory.js"],"sourcesContent":["import { nanoid } from \"nanoid/non-secure\";\nfunction createMemoryHistory() {\n  let index = 0;\n  let items = [];\n  const pending = [];\n  const interrupt = () => {\n    pending.forEach((it) => {\n      const cb = it.cb;\n      it.cb = () => cb(true);\n    });\n  };\n  const history = {\n    get index() {\n      const id = window.history.state?.id;\n      if (id) {\n        const index2 = items.findIndex((item) => item.id === id);\n        return index2 > -1 ? index2 : 0;\n      }\n      return 0;\n    },\n    get(index2) {\n      return items[index2];\n    },\n    backIndex({ path }) {\n      for (let i = index - 1; i >= 0; i--) {\n        const item = items[i];\n        if (item.path === path) {\n          return i;\n        }\n      }\n      return -1;\n    },\n    // @modified - added displayPath and unmaskOnReload parameters for route masking\n    push({\n      path,\n      state,\n      displayPath,\n      unmaskOnReload\n    }) {\n      interrupt();\n      const id = nanoid();\n      items = items.slice(0, index + 1);\n      items.push({ path, state, id, displayPath });\n      index = items.length - 1;\n      const urlPath = displayPath ?? path;\n      if (process.env.ONE_DEBUG_ROUTER) {\n        console.info(\n          `[one] \\u{1F4DC} history.push path=${path}${displayPath ? ` displayPath=${displayPath}` : \"\"}`\n        );\n      }\n      const historyState = { id };\n      if (displayPath && displayPath !== path) {\n        historyState.__tempLocation = {\n          pathname: path,\n          search: \"\",\n          hash: \"\"\n        };\n        if (unmaskOnReload) {\n          historyState.__tempKey = id;\n        }\n      }\n      window.history.pushState(historyState, \"\", urlPath);\n    },\n    // @modified - added displayPath and unmaskOnReload parameters for route masking\n    replace({\n      path,\n      state,\n      displayPath,\n      unmaskOnReload\n    }) {\n      interrupt();\n      const id = window.history.state?.id ?? nanoid();\n      let pathWithHash = path;\n      const hash = pathWithHash.includes(\"#\") ? \"\" : location.hash;\n      if (!items.length || items.findIndex((item) => item.id === id) < 0) {\n        pathWithHash = pathWithHash + hash;\n        items = [{ path: pathWithHash, state, id, displayPath }];\n        index = 0;\n      } else {\n        if (items[index].path === path) {\n          pathWithHash = pathWithHash + hash;\n        }\n        items[index] = { path, state, id, displayPath };\n      }\n      const urlPath = displayPath ? displayPath + hash : pathWithHash;\n      if (process.env.ONE_DEBUG_ROUTER) {\n        console.info(\n          `[one] \\u{1F4DC} history.replace path=${pathWithHash}${displayPath ? ` displayPath=${displayPath}` : \"\"}`\n        );\n      }\n      const historyState = { id };\n      if (displayPath && displayPath !== path) {\n        historyState.__tempLocation = {\n          pathname: path,\n          search: \"\",\n          hash: \"\"\n        };\n        if (unmaskOnReload) {\n          historyState.__tempKey = id;\n        }\n      }\n      window.history.replaceState(historyState, \"\", urlPath);\n    },\n    // `history.go(n)` is asynchronous, there are couple of things to keep in mind:\n    // - it won't do anything if we can't go `n` steps, the `popstate` event won't fire.\n    // - each `history.go(n)` call will trigger a separate `popstate` event with correct location.\n    // - the `popstate` event fires before the next frame after calling `history.go(n)`.\n    // This method differs from `history.go(n)` in the sense that it'll go back as many steps it can.\n    go(n) {\n      interrupt();\n      const nextIndex = index + n;\n      const lastItemIndex = items.length - 1;\n      if (n < 0 && !items[nextIndex]) {\n        n = -index;\n        index = 0;\n      } else if (n > 0 && nextIndex > lastItemIndex) {\n        n = lastItemIndex - index;\n        index = lastItemIndex;\n      } else {\n        index = nextIndex;\n      }\n      if (n === 0) {\n        return;\n      }\n      return new Promise((resolve, reject) => {\n        const done = (interrupted) => {\n          clearTimeout(timer);\n          if (interrupted) {\n            reject(new Error(\"History was changed during navigation.\"));\n            return;\n          }\n          const { title } = window.document;\n          window.document.title = \"\";\n          window.document.title = title;\n          resolve();\n        };\n        pending.push({ ref: done, cb: done });\n        const timer = setTimeout(() => {\n          const index2 = pending.findIndex((it) => it.ref === done);\n          if (index2 > -1) {\n            pending[index2].cb();\n            pending.splice(index2, 1);\n          }\n        }, 100);\n        const onPopState = () => {\n          const id = window.history.state?.id;\n          const currentIndex = items.findIndex((item) => item.id === id);\n          index = Math.max(currentIndex, 0);\n          const last = pending.pop();\n          window.removeEventListener(\"popstate\", onPopState);\n          last?.cb();\n        };\n        window.addEventListener(\"popstate\", onPopState);\n        window.history.go(n);\n      });\n    },\n    // The `popstate` event is triggered when history changes, except `pushState` and `replaceState`\n    // If we call `history.go(n)` ourselves, we don't want it to trigger the listener\n    // Here we normalize it so that only external changes (e.g. user pressing back/forward) trigger the listener\n    listen(listener) {\n      const onPopState = () => {\n        if (pending.length) {\n          return;\n        }\n        const popId = window.history.state?.id;\n        if (popId) {\n          const foundIndex = items.findIndex((item) => item.id === popId);\n          if (foundIndex > -1) {\n            index = foundIndex;\n          }\n        }\n        listener();\n      };\n      window.addEventListener(\"popstate\", onPopState);\n      return () => window.removeEventListener(\"popstate\", onPopState);\n    }\n  };\n  return history;\n}\nexport {\n  createMemoryHistory\n};\n//# sourceMappingURL=createMemoryHistory.js.map\n"],"mappings":";;;AACA,SAAS,sBAAsB;CAC7B,IAAI,QAAQ;CACZ,IAAI,QAAQ,CAAC;CACb,MAAM,UAAU,CAAC;CACjB,MAAM,kBAAkB;EACtB,QAAQ,SAAS,OAAO;GACtB,MAAM,KAAK,GAAG;GACd,GAAG,WAAW,GAAG,IAAI;EACvB,CAAC;CACH;CACA,MAAM,UAAU;EACd,IAAI,QAAQ;GACV,MAAM,KAAK,OAAO,QAAQ,OAAO;GACjC,IAAI,IAAI;IACN,MAAM,SAAS,MAAM,WAAW,SAAS,KAAK,OAAO,EAAE;IACvD,OAAO,SAAS,CAAC,IAAI,SAAS;GAChC;GACA,OAAO;EACT;EACA,IAAI,QAAQ;GACV,OAAO,MAAM;EACf;EACA,UAAU,EAAE,QAAQ;GAClB,KAAK,IAAI,IAAI,QAAQ,GAAG,KAAK,GAAG,KAAK;IACnC,MAAM,OAAO,MAAM;IACnB,IAAI,KAAK,SAAS,MAAM;KACtB,OAAO;IACT;GACF;GACA,OAAO,CAAC;EACV;EAEA,KAAK,EACH,MACA,OACA,aACA,kBACC;GACD,UAAU;GACV,MAAM,KAAK,OAAO;GAClB,QAAQ,MAAM,MAAM,GAAG,QA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