{"version":3,"file":"handlePositioning.mjs","sources":["../../../../packages/chart-line/utils/handlePositioning.ts"],"sourcesContent":["import { checkViewportOverflow } from './checkViewport.js'\nimport { LABEL_HEIGHT_INCREASE, VIEWPORT_MARGIN } from './constants.js'\n\ntype HandlePositioningProps = {\n  previousIds?: string[]\n  id: string\n  reset?: boolean\n  containerId: string\n}\n\ntype PositioningData = {\n  elements: {\n    threshold: HTMLElement\n    label: HTMLElement\n  }\n  rects: {\n    threshold: DOMRect\n    label: DOMRect\n  }\n  containerRect: DOMRect\n}\n\n// Get elements and their rects in one go\nconst getElementData = (\n  id: string,\n  containerId: string,\n): PositioningData | null => {\n  const threshold = document.getElementById(id)\n  const label = document.getElementById(`${id}-description`)\n\n  if (!threshold || !label) return null\n\n  const container = document.getElementById(containerId)\n  if (!container) return null\n\n  // Temporarily reset transform to get original positions\n  const originalTransform = label.style.transform\n  label.style.removeProperty('transform')\n\n  const thresholdRect = threshold.getBoundingClientRect()\n  const labelRect = label.getBoundingClientRect()\n  const containerRect = container.getBoundingClientRect()\n\n  // Restore transform if it existed\n  if (originalTransform) {\n    label.style.setProperty('transform', originalTransform)\n  }\n\n  return {\n    elements: { threshold, label },\n    rects: { threshold: thresholdRect, label: labelRect },\n    containerRect,\n  }\n}\n\n// Calculate offset needed to keep label within viewport bounds\nconst calculateLabelOffset = (\n  thresholdRect: DOMRect,\n  labelRect: DOMRect,\n  containerRect: DOMRect,\n  isInverted: boolean,\n): number => {\n  if (isInverted) {\n    // Label positioned with left: 0 (extends right from threshold)\n    // Check if label extends beyond container right boundary\n    const labelRightEdge = thresholdRect.left + labelRect.width\n    const containerRightEdge = containerRect.right - VIEWPORT_MARGIN\n\n    if (labelRightEdge > containerRightEdge) {\n      return labelRightEdge - containerRightEdge\n    }\n  } else {\n    // Label positioned with right: 0 (extends left from threshold - default behavior)\n    // Check if label left edge goes beyond container left boundary\n    const labelLeftEdge = thresholdRect.right - labelRect.width\n    const containerLeftEdge = containerRect.left + VIEWPORT_MARGIN\n\n    if (labelLeftEdge < containerLeftEdge) {\n      return containerLeftEdge - labelLeftEdge\n    }\n  }\n  return 0\n}\n\n// Calculate if flag should be inverted based on viewport overflow\nconst shouldInvertLabel = (\n  thresholdRect: DOMRect,\n  labelRect: DOMRect,\n  containerRect: DOMRect,\n  viewportWidth: number,\n): boolean => {\n  const position = containerRect.right - thresholdRect.right\n\n  const isInverted = checkViewportOverflow(\n    position,\n    labelRect.width,\n    viewportWidth,\n  )\n\n  return isInverted\n}\n\nexport const resetThresholdHeight = (threshold: HTMLElement | null): void => {\n  if (!threshold) return\n\n  threshold.style.removeProperty('height')\n}\n\n// Apply label positioning based on inversion\nconst applyLabelPosition = (label: HTMLElement, isInverted: boolean): void => {\n  if (isInverted) {\n    label.style.setProperty('left', '0px')\n    label.style.setProperty('right', 'auto')\n    label.dataset.isInverted = 'true'\n  } else {\n    label.style.setProperty('right', '0px')\n    label.style.setProperty('left', 'auto')\n    label.dataset.isInverted = 'false'\n  }\n}\n\n// Calculate distance between two thresholds considering label inversion\nconst calculateFlagDistance = (\n  currentRect: DOMRect,\n  previousRect: DOMRect,\n  currentLabelRect: DOMRect,\n  isPreviousInverted: boolean,\n): number => {\n  return isPreviousInverted\n    ? currentRect.left - previousRect.left - currentLabelRect.width\n    : currentRect.left - previousRect.left\n}\n\n// Get first previous threshold on the same position as the current threshold\nconst getPreviousThreshold = (\n  previousIds: string[],\n  currentId: string,\n): string | null => {\n  const currentElement = document.getElementById(currentId)\n  if (!currentElement) return null\n\n  const currentLabelPosition = currentElement.dataset.labelPosition\n\n  for (let i = previousIds.length - 1; i >= 0; i--) {\n    const id = previousIds[i]\n    const element = document.getElementById(id)\n    if (element?.dataset.labelPosition === currentLabelPosition) {\n      return id\n    }\n  }\n\n  return null\n}\n\nexport const handlePositioning = (props: HandlePositioningProps): void => {\n  const { previousIds = [], id, reset, containerId } = props\n\n  // Check if we are in browser (not SSR)\n  if (typeof window === 'undefined') return\n\n  if (reset) {\n    resetThresholdHeight(document.getElementById(id))\n    return\n  }\n\n  const currentData = getElementData(id, containerId)\n  if (!currentData) return\n\n  const {\n    elements: currentElements,\n    rects: currentRects,\n    containerRect,\n  } = currentData\n\n  // Calculate inversion for current label using container width\n  const currentIsInverted = shouldInvertLabel(\n    currentRects.threshold,\n    currentRects.label,\n    containerRect,\n    containerRect.width,\n  )\n\n  // Apply positioning for current label\n  applyLabelPosition(currentElements.label, currentIsInverted)\n\n  // Calculate offset needed to keep label within bounds\n  const currentOffset = calculateLabelOffset(\n    currentRects.threshold,\n    currentRects.label,\n    containerRect,\n    currentIsInverted,\n  )\n\n  // Apply offset if needed\n  if (currentOffset > 0) {\n    const direction = currentIsInverted ? -currentOffset : currentOffset\n    currentElements.label.style.setProperty(\n      'transform',\n      `translateX(${direction}px)`,\n    )\n  } else {\n    currentElements.label.style.removeProperty('transform')\n  }\n\n  // Handle previous threshold positioning if exists\n  const previousId = getPreviousThreshold(previousIds, id)\n  if (!previousId) return\n\n  const previousData = getElementData(previousId, containerId)\n  if (!previousData) return\n\n  const { rects: previousRects, elements: previousElements } = previousData\n\n  // Calculate inversion for previous flag using window width (as in original)\n  const previousIsInverted =\n    previousElements.label.dataset.isInverted === 'true'\n\n  // Calculate distance and adjust height if needed\n  const distance = calculateFlagDistance(\n    currentRects.threshold,\n    previousRects.threshold,\n    currentRects.label,\n    previousIsInverted,\n  )\n\n  if (distance < currentRects.label.width) {\n    const newHeight = previousRects.threshold.height + LABEL_HEIGHT_INCREASE\n    currentElements.threshold.style.setProperty('height', `${newHeight}px`)\n  }\n}\n"],"names":["getElementData","id","containerId","threshold","document","getElementById","label","container","originalTransform","style","transform","removeProperty","thresholdRect","getBoundingClientRect","labelRect","containerRect","setProperty","elements","rects","calculateLabelOffset","isInverted","labelRightEdge","left","width","containerRightEdge","right","VIEWPORT_MARGIN","labelLeftEdge","containerLeftEdge","shouldInvertLabel","viewportWidth","position","checkViewportOverflow","resetThresholdHeight","applyLabelPosition","dataset","calculateFlagDistance","currentRect","previousRect","currentLabelRect","isPreviousInverted","getPreviousThreshold","previousIds","currentId","currentElement","currentLabelPosition","labelPosition","i","length","element","handlePositioning","props","reset","window","currentData","currentElements","currentRects","currentIsInverted","currentOffset","direction","previousId","previousData","previousRects","previousElements","previousIsInverted","distance","newHeight","height","LABEL_HEIGHT_INCREASE"],"mappings":";;;AAsBA;AACA,MAAMA,cAAc,GAAGA,CACrBC,EAAU,EACVC,WAAmB,KACQ;AAC3B,EAAA,MAAMC,SAAS,GAAGC,QAAQ,CAACC,cAAc,CAACJ,EAAE,CAAC,CAAA;EAC7C,MAAMK,KAAK,GAAGF,QAAQ,CAACC,cAAc,CAAC,CAAA,EAAGJ,EAAE,CAAA,YAAA,CAAc,CAAC,CAAA;AAE1D,EAAA,IAAI,CAACE,SAAS,IAAI,CAACG,KAAK,EAAE,OAAO,IAAI,CAAA;AAErC,EAAA,MAAMC,SAAS,GAAGH,QAAQ,CAACC,cAAc,CAACH,WAAW,CAAC,CAAA;AACtD,EAAA,IAAI,CAACK,SAAS,EAAE,OAAO,IAAI,CAAA;;AAE3B;AACA,EAAA,MAAMC,iBAAiB,GAAGF,KAAK,CAACG,KAAK,CAACC,SAAS,CAAA;AAC/CJ,EAAAA,KAAK,CAACG,KAAK,CAACE,cAAc,CAAC,WAAW,CAAC,CAAA;AAEvC,EAAA,MAAMC,aAAa,GAAGT,SAAS,CAACU,qBAAqB,EAAE,CAAA;AACvD,EAAA,MAAMC,SAAS,GAAGR,KAAK,CAACO,qBAAqB,EAAE,CAAA;AAC/C,EAAA,MAAME,aAAa,GAAGR,SAAS,CAACM,qBAAqB,EAAE,CAAA;;AAEvD;AACA,EAAA,IAAIL,iBAAiB,EAAE;IACrBF,KAAK,CAACG,KAAK,CAACO,WAAW,CAAC,WAAW,EAAER,iBAAiB,CAAC,CAAA;AACzD,GAAA;EAEA,OAAO;AACLS,IAAAA,QAAQ,EAAE;MAAEd,SAAS;AAAEG,MAAAA,KAAAA;KAAO;AAC9BY,IAAAA,KA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