{"version":3,"file":"get-timeline.mjs","sources":["../../../../../../src/render/dom/scroll/utils/get-timeline.ts"],"sourcesContent":["import { ProgressTimeline } from \"motion-dom\"\nimport { scrollInfo } from \"../track\"\nimport { ScrollOptionsWithDefaults } from \"../types\"\nimport { canUseNativeTimeline } from \"./can-use-native-timeline\"\nimport { offsetToViewTimelineRange } from \"./offset-to-range\"\n\ndeclare class ScrollTimeline implements ProgressTimeline {\n    constructor(options: ScrollOptions)\n\n    currentTime: null | { value: number }\n\n    cancel?: VoidFunction\n}\n\ndeclare class ViewTimeline implements ProgressTimeline {\n    constructor(options: { subject: Element; axis?: string })\n\n    currentTime: null | { value: number }\n\n    cancel?: VoidFunction\n}\n\nconst timelineCache = new Map<\n    Element,\n    Map<Element | \"self\", Record<string, ProgressTimeline>>\n>()\n\nfunction scrollTimelineFallback(options: ScrollOptionsWithDefaults) {\n    const currentTime = { value: 0 }\n\n    const cancel = scrollInfo((info) => {\n        currentTime.value = info[options.axis!].progress * 100\n    }, options)\n\n    return { currentTime, cancel }\n}\n\nexport function getTimeline({\n    source,\n    container,\n    ...options\n}: ScrollOptionsWithDefaults): ProgressTimeline {\n    const { axis } = options\n\n    if (source) container = source\n\n    let containerCache = timelineCache.get(container)\n    if (!containerCache) {\n        containerCache = new Map()\n        timelineCache.set(container, containerCache)\n    }\n\n    const targetKey = options.target ?? \"self\"\n    let targetCache = containerCache.get(targetKey)\n    if (!targetCache) {\n        targetCache = {}\n        containerCache.set(targetKey, targetCache)\n    }\n\n    const axisKey = axis + (options.offset ?? []).join(\",\")\n\n    if (!targetCache[axisKey]) {\n        if (options.target && canUseNativeTimeline(options.target)) {\n            const range = offsetToViewTimelineRange(options.offset)\n            if (range) {\n                targetCache[axisKey] = new ViewTimeline({\n                    subject: options.target,\n                    axis,\n                })\n            } else {\n                targetCache[axisKey] = scrollTimelineFallback({\n                    container,\n                    ...options,\n                })\n            }\n        } else if (canUseNativeTimeline()) {\n            targetCache[axisKey] = new ScrollTimeline({\n                source: container,\n                axis,\n            } as any)\n        } else {\n            targetCache[axisKey] = scrollTimelineFallback({\n                container,\n                ...options,\n            })\n        }\n    }\n\n    return targetCache[axisKey]!\n}\n"],"names":[],"mappings":";;;;AAsBA,MAAM,aAAa,GAAG,IAAI,GAAG,EAG1B;AAEH,SAAS,sBAAsB,CAAC,OAAkC,EAAA;AAC9D,IAAA,MAAM,WAAW,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE;AAEhC,IAAA,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,IAAI,KAAI;AAC/B,QAAA,WAAW,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,IAAK,CAAC,CAAC,QAAQ,GAAG,GAAG;IAC1D,CAAC,EAAE,OAAO,CAAC;AAEX,IAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE;AAClC;AAEM,SAAU,WAAW,CAAC,EACxB,MAAM,EACN,SAAS,EACT,GAAG,OAAO,EACc,EAAA;AACxB,IAAA,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO;AAExB,IAAA,IAAI,MAAM;QAAE,SAAS,GAAG,MAAM;IAE9B,IAAI,cAAc,GAAG,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC;IACjD,IAAI,CAAC,cAAc,EAAE;AACjB,QAAA,cAAc,GAAG,IAAI,GAAG,EAAE;AAC1B,QAAA,aAAa,CAAC,GAAG,CAAC,SAAS,EAAE,cAAc,CAAC;IAChD;AAEA,IAAA,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,IAAI,MAAM;IAC1C,IAAI,WAAW,GAAG,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC;IAC/C,IAAI,CAAC,WAAW,EAAE;QACd,WAAW,GAAG,EAAE;AAChB,QAAA,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC;IAC9C;AAEA,IAAA,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,EAAE,IAAI,CAAC,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