{"version":3,"file":"index.cjs","sources":["../../../src/containers/xy-container/index.tsx"],"sourcesContent":["import { XYContainer } from '@unovis/ts'\nimport type {\n  Annotations,\n  Axis,\n  Crosshair,\n  Tooltip,\n  XYComponentCore,\n  XYContainerConfigInterface,\n} from '@unovis/ts'\nimport type { JSX, ParentProps } from 'solid-js'\nimport {\n  createEffect,\n  createSignal,\n  on,\n  onCleanup,\n  onMount,\n  splitProps,\n} from 'solid-js'\nimport { createStore, produce } from 'solid-js/store'\nimport type { VisContainerContextProps } from '../../utils/context'\nimport { VisContainerContext } from '../../utils/context'\nimport { createTrigger } from '../../utils/trigger'\nimport { combineStyle } from '../../utils/combine-style'\n\nexport type VisXYContainerProps<Datum> = ParentProps<\n  XYContainerConfigInterface<Datum> & {\n    data?: Datum[]\n    class?: string\n    style?: JSX.CSSProperties\n  }\n>\n\nexport function VisXYContainer<Datum>(props: VisXYContainerProps<Datum>) {\n  const [div, style, data, rest] = splitProps(\n    props,\n    ['children', 'class'],\n    ['style'],\n    ['data']\n  )\n  const [ref, setRef] = createSignal<HTMLDivElement>()\n  const [chart, setChart] = createSignal<XYContainer<Datum>>()\n  const [config, setConfig] = createStore<XYContainerConfigInterface<Datum>>({\n    components: [],\n    annotations: undefined,\n    crosshair: undefined,\n    tooltip: undefined,\n    xAxis: undefined,\n    yAxis: undefined,\n  })\n  const [track, dirty] = createTrigger()\n\n  onMount(() => {\n    const r = ref()\n    if (r) setChart(new XYContainer(r, config, data.data))\n  })\n\n  onCleanup(() => chart()?.destroy())\n\n  createEffect(\n    on(\n      () => data.data,\n      (data) => {\n        if (data) chart()?.setData(data)\n      }\n    )\n  )\n\n  createEffect(() => {\n    // track the changes\n    track()\n    chart()?.updateContainer({ ...config, ...rest })\n  })\n\n  const update: VisContainerContextProps['update'] = (key, value) => {\n    setConfig(\n      produce((c) => {\n        switch (key) {\n          case 'component':\n            c.components = [\n              ...(c.components as XYComponentCore<Datum>[]),\n              value() as XYComponentCore<Datum>,\n            ]\n            break\n          case 'axis':\n            // @ts-expect-error -- ts does not happy with the type\n            // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n            c[`${(value() as Axis<Datum>).config.type}Axis`] =\n              value() as Axis<Datum>\n            break\n          case 'crosshair':\n            c.crosshair = value() as Crosshair<Datum>\n            break\n          case 'annotations':\n            c.annotations = value() as Annotations\n            break\n          case 'tooltip':\n            c.tooltip = value() as Tooltip\n        }\n      })\n    )\n  }\n\n  const destroy: VisContainerContextProps['destroy'] = (key, value) => {\n    setConfig(\n      produce((c) => {\n        switch (key) {\n          case 'component':\n            c.components = c.components?.filter((i) => !i.isDestroyed())\n            break\n          case 'axis':\n            // @ts-expect-error -- ts does not happy with the type\n            // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n            c[`${value}Axis`] = undefined\n            break\n          case 'crosshair':\n            c.crosshair = undefined\n            break\n          case 'annotations':\n            c.annotations = undefined\n            break\n          case 'tooltip':\n            c.tooltip = undefined\n        }\n      })\n    )\n  }\n\n  return (\n    <VisContainerContext.Provider value={{ update, destroy, dirty }}>\n      <div\n        data-vis-xy-container\n        ref={setRef}\n        style={combineStyle(\n          {\n            display: 'block',\n            position: 'relative',\n            width: '100%',\n          },\n          style.style\n        )}\n        {...div}\n      />\n    </VisContainerContext.Provider>\n  )\n}\n"],"names":["VisXYContainer","props","div","style","data","rest","splitProps","ref","setRef","createSignal","chart","setChart","config","setConfig","createStore","components","annotations","undefined","crosshair","tooltip","xAxis","yAxis","track","dirty","createTrigger","onMount","r","XYContainer","onCleanup","destroy","createEffect","on","setData","updateContainer","update","key","value","produce","c","type","filter","i","isDestroyed","_$createComponent","VisContainerContext","Provider","children","_el$","_tmpl$","_$use","_$spread","_$mergeProps","combineStyle","display","position","width"],"mappings":"+VAgCO,SAASA,EAAsBC,EAAmC,CACvE,KAAM,CAACC,EAAKC,EAAOC,EAAMC,CAAI,EAAIC,EAAAA,WAC/BL,EACA,CAAC,WAAY,OAAO,EACpB,CAAC,OAAO,EACR,CAAC,MAAM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