import a from "react";
import { w as G, G as n, x as V, y as _, z as B, A as J, D as T, a as P, E as W, J as $, K as q, N as H } from "./App-7cdde89e.mjs";
import "@emotion/react";
import "react-dom";
import "@emotion/styled";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
function ee() {
  const k = G(), f = n.useSelectState((e) => e.filterList), y = n.useSelectState((e) => e.sortList), {
    table: o,
    insight: s
  } = V(), l = n.useSelectState((e) => e.rowsContext), S = n.useSelectState((e) => e.columnsContext), C = n.useSelectState((e) => e.intervalDateInsight.start), w = n.useSelectState((e) => e.intervalDateInsight.end), x = n.useSelectState((e) => e.parameters), {
    setRowsContext: N,
    setColumnsContext: K,
    setRevalidateRow: L,
    setInsightConfig: j,
    addFilterList: M,
    addSortList: O,
    setParameters: A,
    setRevalidateRowIsValidating: g
  } = n.useActions(), d = _(), D = a.useMemo(() => B.reduce(x, (e, t) => (e[t.options.name] = t.options.value, e), {}), [x]), z = a.useCallback((e) => {
    var u;
    const t = (e == null ? void 0 : e.registeredParams) || {};
    if (!t)
      return;
    const r = e != null && e.registeredParams ? Object.keys(e == null ? void 0 : e.registeredParams) : [], m = (u = e == null ? void 0 : e.params) == null ? void 0 : u.filter((p) => r.length ? r.find((F) => F !== p) : !0);
    return m == null || m.forEach((p) => {
      e && (t[p] = {
        default: "",
        type: ""
      });
    }), t;
  }, []), c = a.useMemo(() => {
    const e = o == null ? void 0 : o.insights.find((r) => r.name === d.insightName);
    return {
      orderBy: Object.fromEntries((e == null ? void 0 : e.sorts.map((r) => {
        const [m] = Object.keys(r).filter((u) => typeof r[u] == "string");
        return [m, r[m]];
      })) || []),
      condition: e == null ? void 0 : e.baseCondition,
      params: z(e)
    };
  }, [o == null ? void 0 : o.insights, d.insightName]);
  a.useEffect(() => {
    M(c.condition), O(c.orderBy), c.params && A(Object.entries(c.params).map(([e, t]) => ({
      id: J(),
      options: {
        name: e,
        default: t == null ? void 0 : t.default,
        ...(t == null ? void 0 : t.enum) && {
          enum: t == null ? void 0 : t.enum
        },
        value: (t == null ? void 0 : t.default) || ""
      },
      type: t == null ? void 0 : t.type
    })));
  }, [c.condition, c.params, c.orderBy, d.insightName]);
  const {
    data: i,
    mutate: R,
    isValidating: I
  } = T([k, s, C, w, f, y, D, "insight-row"], async (e) => {
    try {
      return (await e.execute({
        operations: [{
          operation: "Insight",
          instruction: {
            name: `${s == null ? void 0 : s.name}`,
            insight: s == null ? void 0 : s.name,
            start: C,
            end: w,
            condition: f,
            params: D,
            orderBy: B.omit(y, ["id"])
          }
        }]
      })).data;
    } catch {
      return {
        results: null
      };
    }
  }, {
    suspense: !0
  });
  a.useEffect(() => {
    g(I);
  }, [I]);
  const E = a.useMemo(() => {
    const e = (s == null ? void 0 : s.columns.map((t, r) => ({
      type: t.type,
      accessorKey: t.name,
      description: t.description,
      isDisabled: !0,
      idx: r + 1
    }))) || [];
    return [{
      type: "rowId",
      accessorKey: "rowId",
      size: 90,
      isDisabled: !0
    }, ...e];
  }, [o, d.insightName]);
  a.useEffect(() => {
    K(E.map((e) => ({
      ...e,
      isShown: !0
    }))), s != null && s.insight && j(s.insight);
  }, [E]);
  const b = a.useMemo(() => S.filter((e) => e.isShown === !0 && e.accessorKey), [S]);
  return a.useEffect(() => {
    var e, t;
    N(((t = (e = i == null ? void 0 : i.results) == null ? void 0 : e[(s == null ? void 0 : s.name) || ""]) == null ? void 0 : t.map((r) => ({
      ...r,
      isCreated: !0
    }))) || []), L(R);
  }, [i, R]), !l.length && !b.length ? /* @__PURE__ */ P(W, {}) : /* @__PURE__ */ P($, {
    columns: q(b),
    data: l,
    tableName: o == null ? void 0 : o.name,
    variant: "showFooter",
    defaultColumn: H
  });
}
export {
  ee as default
};
