{"version":3,"file":"InsightRenderer.mjs","names":["React","useMemo","useState","useEffect","UdpCharts","Insight","InsightTypeField","InsightTypeProperties","useUser","useAxiosGet","QueryResult","ConfigService","UdpChart","InsightRendererProps","insight","data","insightId","capitilizeFirstLetter","s","charAt","toUpperCase","slice","decapitilizeFirstLetter","toLowerCase","getAggregateValue","key","T","aggregateResults","InsightRenderer","FC","props","insightProp","user","loadedInsight","integrationV2ApiUrl","Chart","insightInsightType","properties","JSON","parse","component","kpis","value","label","active","undefined","insightInsightKpi","map","kpi","kpiField","operation","property","field","console","log","dataConfig","dataMapping","name","alias","dataFields","reduce","fieldValue","find","dm","combinedData","length","pageList","page","i","title","subheader","filter"],"sources":["../../../src/UI/charts/InsightRenderer.tsx"],"sourcesContent":["import React, { useMemo, useState, useEffect } from 'react';\nimport * as UdpCharts  from './amcharts';\nimport { Insight, InsightTypeField, InsightTypeProperties } from './types';\nimport { useUser } from '../../utilities/auth/useUser';\nimport { useAxiosGet } from '../../utilities/useAxiosGet';\nimport QueryResult from '../../models/QueryResult';\nimport { ConfigService } from '../../configService';\nexport type UdpChart =\n  | 'UdpBarChart'\n  | 'UdpDonutChart'\n  | 'UdpLineChart'\n  | 'UdpDistributionChart';\nexport interface InsightRendererProps {\n  insight: Insight | undefined;\n  data: QueryResult | undefined;\n  insightId: string | undefined;\n}\n\nconst capitilizeFirstLetter = (s: string): string =>\n  s?.charAt?.(0)?.toUpperCase?.() + s?.slice?.(1);\nconst decapitilizeFirstLetter = (s: string): string =>\n  s?.charAt?.(0)?.toLowerCase?.() + s?.slice?.(1);\n\nfunction getAggregateValue<T>(data: QueryResult | undefined, key: string): T {\n  return data?.aggregateResults?.[0]?.[key] as T;\n}\n/**\n * InsightRenderer component\n *\n * This component is responsible for rendering insights based on the provided props.\n *\n * @prop {Insight} `insight` - The insight metadata\n * @prop {any} `data` - Search results associated with the insight\n * @prop {string} `insightId` - InsightId, if included the component will get the data from server\n * @returns {JSX.Element} - The rendered insight component.\n */\nexport const InsightRenderer: React.FC<InsightRendererProps> = (props: InsightRendererProps) => {\n  const { insight: insightProp, data, insightId } = props;\n  const user = useUser();\n  const [{ data: loadedInsight }] = useAxiosGet(\n    ConfigService.integrationV2ApiUrl,\n    `Insight/${insightId}/Full`,\n    {},\n    !insightId || !user\n  ) as unknown as [{ data: Insight }];\n\n  // Use the insight passed in or the insightId\n  const insight = useMemo(() => {\n    if (!!insightId) return loadedInsight;\n    else return insightProp;\n  }, [loadedInsight, insightId, insightProp]);\n\n  // determine chart component\n  const Chart = useMemo(() => {\n    if (!insight?.insightInsightType?.properties) return null;\n    let properties: InsightTypeProperties;\n    if (typeof insight?.insightInsightType?.properties === 'string') {\n      properties = JSON.parse(\n        insight?.insightInsightType?.properties as unknown as string\n      ) as InsightTypeProperties;\n    } else {\n      properties = insight?.insightInsightType?.properties;\n    }\n    // @ts-ignore\n    return UdpCharts?.[properties?.component];\n  }, [insight?.insightInsightType]);\n\n  const kpis:\n    | { value?: string; label?: string; active?: boolean }[]\n    | undefined = useMemo(() => {\n    if (insight == null) return undefined;\n    return insight?.insightInsightKpi?.map?.((kpi) => {\n      const kpiField: { operation?: string; property?: string } =\n        typeof kpi.field === 'object' ? kpi.field : JSON.parse(kpi.field);\n      console.log(kpiField);\n      return {\n        label: kpi.label,\n        active: kpi.active,\n        value: getAggregateValue<string>(\n          data,\n          `${\n            decapitilizeFirstLetter(kpiField.property ?? '') ?? ''\n          }${capitilizeFirstLetter(kpiField.operation?.toLowerCase() ?? '')}`\n        )\n      };\n    });\n  }, [data, insight?.insightInsightKpi]);\n\n  const dataConfig: { [key: string]: any } | null = useMemo(() => {\n    if (insight == null) return null;\n    const dataMapping: {\n      name: string;\n      value: { name: string; operation: string | undefined };\n      alias?: string;\n    }[] =\n      typeof insight.dataMapping === 'object'\n        ? insight.dataMapping\n        : JSON.parse(insight.dataMapping ?? '{}');\n    const dataFields: InsightTypeField[] =\n      typeof insight.insightInsightType?.dataFields === 'object'\n        ? insight.insightInsightType?.dataFields\n        : JSON.parse(insight.insightInsightType?.dataFields ?? '{}');\n\n    return dataFields?.reduce?.((value: { [key: string]: any }, field) => {\n      const fieldValue = dataMapping?.find?.(\n        (dm) => dm.name === field.name\n      )?.value;\n      if (fieldValue?.operation != undefined) {\n        value[field.name] =\n          decapitilizeFirstLetter(fieldValue?.name) + fieldValue.operation;\n      } else {\n        // @ts-ignore\n        value[field.name] = fieldValue?.alias ?? fieldValue?.name ?? fieldValue;\n      }\n\n      return value;\n    }, {});\n  }, [insight?.dataMapping, insight?.insightInsightType]);\n  const combinedData = useMemo(() => {\n    if (data?.aggregateResults == null || data?.aggregateResults?.length === 0)\n      return data?.pageList;\n    return data?.pageList.map((page, i) => ({\n      ...page,\n      ...data?.aggregateResults?.[i]\n    }));\n  }, [data]);\n  if (Chart == null) return null;\n  return (\n    <Chart\n      data={combinedData}\n      title={insight?.title}\n      subheader={insight?.subheader}\n      {...dataConfig}\n      kpis={kpis?.filter((kpi) => kpi.active)}\n    />\n  );\n};\n"],"mappings":";;;;;;AAkBA,MAAMiB,yBAAyBC,MAC7BA,GAAGC,SAAS,EAAE,EAAEC,eAAe,GAAGF,GAAGG,QAAQ,EAAE;AACjD,MAAMC,2BAA2BJ,MAC/BA,GAAGC,SAAS,EAAE,EAAEI,eAAe,GAAGL,GAAGG,QAAQ,EAAE;AAEjD,SAASG,kBAAqBT,MAA+BU,KAAgB;AAC3E,QAAOV,MAAMY,mBAAmB,KAAKF;;;;;;;;;;;;AAYvC,MAAaG,mBAAmDE,UAAgC;CAC9F,MAAM,EAAEhB,SAASiB,aAAahB,MAAMC,cAAcc;CAClD,MAAME,OAAOxB,SAAS;CACtB,MAAM,CAAC,EAAEO,MAAMkB,mBAAmBxB,YAChCE,cAAcuB,qBACd,WAAWlB,UAAS,QACpB,EAAE,EACF,CAACA,aAAa,CAACgB,KAChB;CAGD,MAAMlB,UAAUb,cAAc;AAC5B,MAAI,CAAC,CAACe,UAAW,QAAOiB;MACnB,QAAOF;IACX;EAACE;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