{"version":3,"file":"table-summary.cjs","sources":["../../../components/table/table-summary.ts"],"sourcesContent":["import { defineComponent, inject, onBeforeUnmount, reactive, renderSlot, watch } from 'vue'\n\nimport { useProps } from '@vexip-ui/config'\nimport { isNull } from '@vexip-ui/utils'\nimport { tableSummaryProps } from './props'\nimport { TABLE_ACTIONS } from './symbol'\n\nimport type { SummaryWithKey } from './symbol'\n\ntype SummaryPropKey = keyof typeof tableSummaryProps\n\nconst propKeys = Object.keys(tableSummaryProps) as SummaryPropKey[]\nconst aliases: Partial<Record<SummaryPropKey, string>> = {\n  idKey: 'key',\n}\nconst ignoredProps: SummaryPropKey[] = ['renderer']\nconst triggerProps: SummaryPropKey[] = ['idKey', 'cellSpan', 'order', 'above']\n\nconst funcProp = {\n  default: null,\n  isFunc: true,\n  static: true,\n}\n\nexport default defineComponent({\n  name: 'TableSummary',\n  props: tableSummaryProps,\n  setup(_props, { slots }) {\n    const props = useProps('tableSummary', _props, {\n      idKey: {\n        default: null,\n        validator: value => !isNull(value),\n        static: true,\n      },\n      class: null,\n      style: null,\n      attrs: null,\n      cellSpan: funcProp,\n      order: {\n        default: 0,\n        static: true,\n      },\n      above: {\n        default: false,\n        static: true,\n      },\n      meta: null,\n      renderer: funcProp,\n    })\n\n    const tableAction = inject(TABLE_ACTIONS, null)\n    const options = reactive({}) as SummaryWithKey\n\n    for (const key of propKeys) {\n      if (ignoredProps.includes(key)) continue\n\n      const aliasKey = (aliases[key] || key) as keyof SummaryWithKey\n      const trigger = triggerProps.includes(key)\n\n      ;(options[aliasKey] as any) = props[key]\n\n      watch(\n        () => props[key],\n        value => {\n          ;(options[aliasKey] as any) = value\n          trigger\n            ? tableAction?.updateSummaries()\n            : tableAction?.setSummaryProp(options.key, key, value)\n        },\n      )\n    }\n\n    watch(() => props.renderer, setRenderer)\n\n    setRenderer()\n    tableAction?.increaseSummary(options)\n\n    onBeforeUnmount(() => {\n      tableAction?.decreaseSummary(options)\n    })\n\n    function setRenderer() {\n      options.renderer = data => {\n        if (typeof data.column.summaryRenderer === 'function') {\n          return data.column.summaryRenderer({\n            ...data,\n            summary: options,\n          })\n        }\n\n        if (typeof slots.default === 'function') {\n          return renderSlot(slots, 'default', data)\n        }\n\n        if (typeof props.renderer === 'function') {\n          return props.renderer(data)\n        }\n\n        return ''\n      }\n    }\n\n    return () => null\n  },\n})\n"],"names":["propKeys","tableSummaryProps","aliases","ignoredProps","triggerProps","funcProp","TableSummary","defineComponent","_props","slots","props","useProps","value","isNull","tableAction","inject","TABLE_ACTIONS","options","reactive","key","aliasKey","trigger","watch","setRenderer","onBeforeUnmount","data","renderSlot"],"mappings":"kJAWMA,EAAW,OAAO,KAAKC,mBAAiB,EACxCC,EAAmD,CACvD,MAAO,KACT,EACMC,EAAiC,CAAC,UAAU,EAC5CC,EAAiC,CAAC,QAAS,WAAY,QAAS,OAAO,EAEvEC,EAAW,CACf,QAAS,KACT,OAAQ,GACR,OAAQ,EACV,EAEAC,EAAeC,kBAAgB,CAC7B,KAAM,eACN,MAAON,EAAA,kBACP,MAAMO,EAAQ,CAAE,MAAAC,GAAS,CACjB,MAAAC,EAAQC,EAAAA,SAAS,eAAgBH,EAAQ,CAC7C,MAAO,CACL,QAAS,KACT,UAAWI,GAAS,CAACC,EAAAA,OAAOD,CAAK,EACjC,OAAQ,EACV,EACA,MAAO,KACP,MAAO,KACP,MAAO,KACP,SAAUP,EACV,MAAO,CACL,QAAS,EACT,OAAQ,EACV,EACA,MAAO,CACL,QAAS,GACT,OAAQ,EACV,EACA,KAAM,KACN,SAAUA,CAAA,CACX,EAEKS,EAAcC,EAAAA,OAAOC,EAAA,cAAe,IAAI,EACxCC,EAAUC,EAAS,SAAA,EAAE,EAE3B,UAAWC,KAAOnB,EAAU,CACtB,GAAAG,EAAa,SAASgB,CAAG,EAAG,SAE1B,MAAAC,EAAYlB,EAAQiB,CAAG,GAAKA,EAC5BE,EAAUjB,EAAa,SAASe,CAAG,EAEvCF,EAAQG,CAAQ,EAAYV,EAAMS,CAA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