{"version":3,"file":"backdropStack.cjs","names":["_createMessageBus","require","_createStore","noOpenBackdrops","noOpenBackdropIds","getOpenBackdropsWithout","backdropId","openBackdrops","filter","openBackdrop","createBackdropStack","store","createStore","openBackdropIds","openBackdropIdsBus","createMessageBus","getOpenBackdropIds","getState","getOpenBackdrops","setOpenBackdrops","setState","map","publish","closeBackdrop","includes","layer","otherOpenBackdrops","higherLayerIndex","findIndex","insertionIndex","length","slice","concat","subscribeToOpenBackdropIds","subscribe","exports","defaultBackdropStack"],"sources":["../../src/backdropStack.ts"],"sourcesContent":["/*!\n * Copyright (c) 2026-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport {\n  createMessageBus,\n  type UnsubscribeMessageSubscription,\n} from \"./tools/createMessageBus.js\";\nimport { createStore } from \"./tools/createStore.js\";\n\nexport type CloseBackdrop = () => void;\n\nexport type OpenBackdropArgs = {\n  backdropId: string;\n  /**\n   * The z-index the overlay renders at. A higher layer paints above a lower one no matter which opened first, so it decides the order before opening order does.\n   */\n  layer: number;\n};\n\nexport type CloseBackdropArgs = Pick<OpenBackdropArgs, \"backdropId\">;\n\nexport type BackdropStack = {\n  /**\n   * The ids of every open backdrop, bottom first: by layer, then by opening order. The last one is on top. The array keeps its identity until the stack changes, so it is safe to return from a `useSyncExternalStore` snapshot.\n   */\n  getOpenBackdropIds: () => readonly string[];\n  /**\n   * Put a backdrop on top of every open backdrop in its layer. Returns the function that takes it back off.\n   */\n  openBackdrop: (args: OpenBackdropArgs) => CloseBackdrop;\n  subscribeToOpenBackdropIds: (\n    listener: () => void,\n  ) => UnsubscribeMessageSubscription;\n};\n\nconst noOpenBackdrops: readonly OpenBackdropArgs[] = [];\nconst noOpenBackdropIds: readonly string[] = [];\n\nconst getOpenBackdropsWithout = ({\n  backdropId,\n  openBackdrops,\n}: CloseBackdropArgs & { openBackdrops: readonly OpenBackdropArgs[] }) =>\n  openBackdrops.filter(\n    (openBackdrop) => openBackdrop.backdropId !== backdropId,\n  );\n\n/**\n * Create a stack of open backdrops, so stacked overlays can agree on which one paints.\n */\nexport const createBackdropStack = (): BackdropStack => {\n  const store = createStore<{\n    openBackdropIds: readonly string[];\n    openBackdrops: readonly OpenBackdropArgs[];\n  }>({\n    openBackdropIds: noOpenBackdropIds,\n    openBackdrops: noOpenBackdrops,\n  });\n\n  const openBackdropIdsBus = createMessageBus();\n\n  const getOpenBackdropIds = () =>\n    store.getState(\"openBackdropIds\") ?? noOpenBackdropIds;\n\n  const getOpenBackdrops = () =>\n    store.getState(\"openBackdrops\") ?? noOpenBackdrops;\n\n  const setOpenBackdrops = (openBackdrops: readonly OpenBackdropArgs[]) => {\n    store.setState(\"openBackdrops\", openBackdrops);\n    // Derived once here rather than on read, so the snapshot keeps its identity between changes.\n    store.setState(\n      \"openBackdropIds\",\n      openBackdrops.map((openBackdrop) => openBackdrop.backdropId),\n    );\n    openBackdropIdsBus.publish();\n  };\n\n  const closeBackdrop = ({ backdropId }: CloseBackdropArgs) => {\n    if (getOpenBackdropIds().includes(backdropId)) {\n      setOpenBackdrops(\n        getOpenBackdropsWithout({\n          backdropId,\n          openBackdrops: getOpenBackdrops(),\n        }),\n      );\n    }\n  };\n\n  return {\n    getOpenBackdropIds,\n    openBackdrop: ({ backdropId, layer }) => {\n      const otherOpenBackdrops = getOpenBackdropsWithout({\n        backdropId,\n        openBackdrops: getOpenBackdrops(),\n      });\n\n      const higherLayerIndex = otherOpenBackdrops.findIndex(\n        (openBackdrop) => openBackdrop.layer > layer,\n      );\n\n      const insertionIndex =\n        higherLayerIndex === -1 ? otherOpenBackdrops.length : higherLayerIndex;\n\n      setOpenBackdrops(\n        otherOpenBackdrops\n          .slice(0, insertionIndex)\n          .concat({ backdropId, layer })\n          .concat(otherOpenBackdrops.slice(insertionIndex)),\n      );\n\n      return () => closeBackdrop({ backdropId });\n    },\n    subscribeToOpenBackdropIds: openBackdropIdsBus.subscribe,\n  };\n};\n\n/**\n * The one stack every Odyssey overlay in this bundle shares.\n */\nexport const defaultBackdropStack = createBackdropStack();\n"],"mappings":";;;;;;AAYA,IAAAA,iBAAA,GAAAC,OAAA;AAIA,IAAAC,YAAA,GAAAD,OAAA;AAhBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAkCA,MAAME,eAA4C,GAAG,EAAE;AACvD,MAAMC,iBAAoC,GAAG,EAAE;AAE/C,MAAMC,uBAAuB,GAAGA,CAAC;EAC/BC,UAAU;EACVC;AACkE,CAAC,KACnEA,aAAa,CAACC,MAAM,CACjBC,YAAY,IAAKA,YAAY,CAACH,UAAU,KAAKA,UAChD,CAAC;AAKI,MAAMI,mBAAmB,GAAGA,CAAA,KAAqB;EACtD,MAAMC,KAAK,GAAG,IAAAC,wBAAW,EAGtB;IACDC,eAAe,EAAET,iBAAiB;IAClCG,aAAa,EAAEJ;EACjB,CAAC,CAAC;EAEF,MAAMW,kBAAkB,GAAG,IAAAC,kCAAgB,EAAC,CAAC;EAE7C,MAAMC,kBAAkB,GAAGA,CAAA,KACzBL,KAAK,CAACM,QAAQ,CAAC,iBAAiB,CAAC,IAAIb,iBAAiB;EAExD,MAAMc,gBAAgB,GAAGA,CAAA,KACvBP,KAAK,CAACM,QAAQ,CAAC,eAAe,CAAC,IAAId,eAAe;EAEpD,MAAMgB,gBAAgB,GAAIZ,aAA0C,IAAK;IACvEI,KAAK,CAACS,QAAQ,CAAC,eAAe,EAAEb,aAAa,CAAC;IAE9CI,KAAK,CAACS,QAAQ,CACZ,iBAAiB,EACjBb,aAAa,CAACc,GAAG,CAAEZ,YAAY,IAAKA,YAAY,CAACH,UAAU,CA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