import c from "react";
import { G as m, s as O, t as w, ax as z, v as G, w as E, y as j, x as k, l as U, A as H, E as J } from "./App-1cb933e1.mjs";
import "react-dom";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
const Q = /* @__PURE__ */ new Set(["id", "created_at", "updated_at", "auth_provider"]), X = c.lazy(() => import("./App-1cb933e1.mjs").then((o) => o.bl)), Y = (o) => {
  var n;
  if (!o.query)
    return;
  const p = o.query.registeredParams || {}, y = o.query.registeredParams ? Object.keys(o.query.registeredParams) : [], r = (n = o.query.paramsRequired) == null ? void 0 : n.filter((f) => y.length ? y.find((S) => S !== f) : !0);
  return r == null || r.forEach((f) => {
    o.query && (p[f] = {
      default: "",
      type: ""
    });
  }), p;
};
function ee() {
  const o = m.useSelectState((e) => e.sortList), p = m.useSelectState((e) => e.filterList), y = O(), {
    table: r,
    grid: n
  } = w(), f = m.useSelectState((e) => e.rowsContext), S = m.useSelectState((e) => e.columnsContext), {
    limit: h,
    offset: x,
    setIsScrollTable: _
  } = z(), K = m.useSelectState((e) => e.rowsDraft), N = m.useSelectState((e) => e.parameters), q = m.useSelectState((e) => e.viewFields), {
    setColumnsContext: R,
    setRowsContext: F,
    setRevalidateRow: L,
    setRevalidateRowIsValidating: I,
    setParameters: M,
    addFilterList: T,
    addSortList: V,
    setViewFields: W
  } = m.useActions(), i = G(), D = c.useMemo(() => E.reduce(N, (e, a) => (e[a.options.name] = a.options.value, e), {}), [N]), t = c.useMemo(() => {
    var e, a, s, d;
    if (n)
      return {
        orderBy: (e = n.query) == null ? void 0 : e.orderBy,
        condition: (a = n.query) == null ? void 0 : a.condition,
        fields: (s = n.query) == null ? void 0 : s.fields,
        populate: (d = n.query) == null ? void 0 : d.populate,
        params: Y(n)
      };
  }, [r == null ? void 0 : r.views, i.viewName, n]), l = c.useMemo(() => {
    const e = (r == null ? void 0 : r.columns.map((a, s) => {
      var d;
      return {
        type: a.references ? "relation" : a.type,
        accessorKey: a.name,
        references: a.references,
        definition: a.definition,
        description: a.description,
        isDisabled: !!Q.has(a.name),
        size: ((d = a == null ? void 0 : a.metadata) == null ? void 0 : d.width) || 200,
        idx: s + 1
      };
    })) || [];
    return [{
      type: "rowId",
      accessorKey: "rowId",
      size: 90,
      isDisabled: !1,
      idx: 0
    }, ...e];
  }, [r]), {
    data: u,
    mutate: B,
    isValidating: P
  } = j([y, o, p, h, x, D, r == null ? void 0 : r.name, n == null ? void 0 : n.name, i.tableName, i.viewName, "data-grid"], async (e) => {
    if ((r == null ? void 0 : r.name) !== i.tableName || (n == null ? void 0 : n.name) !== i.viewName)
      return {
        results: null
      };
    try {
      const a = r != null && r.name ? {
        name: "data",
        orderBy: o,
        condition: p,
        limit: h,
        offset: x,
        params: D,
        table: r == null ? void 0 : r.name,
        view: n == null ? void 0 : n.name
      } : {
        name: "data",
        orderBy: o,
        condition: p,
        limit: h,
        offset: x,
        params: D,
        table: r == null ? void 0 : r.name
      };
      return (await e.execute({
        operations: [{
          operation: "Select",
          instruction: a
        }]
      })).data;
    } catch {
      return {
        results: null
      };
    }
  }, {
    suspense: !0
  });
  c.useEffect(() => {
    I(P);
  }, [P]), c.useEffect(() => {
    let e = [];
    return i.viewName && (T(t == null ? void 0 : t.condition), V(t == null ? void 0 : t.orderBy), e = (t == null ? void 0 : t.fields) || [], t != null && t.params && M(Object.entries(t == null ? void 0 : t.params).map(([a, s]) => ({
      id: k(),
      options: {
        name: a,
        default: s == null ? void 0 : s.default,
        ...(s == null ? void 0 : s.enum) && {
          enum: s.enum
        },
        ...(s == null ? void 0 : s.inferredWith) && {
          inferredWith: s.inferredWith
        },
        value: (s == null ? void 0 : s.default) || ""
      },
      type: s.type === "float" || s.type === "integer" ? "number" : s == null ? void 0 : s.type
    })))), W(e), () => {
      _(!1);
    };
  }, [t == null ? void 0 : t.condition, t == null ? void 0 : t.params, t == null ? void 0 : t.fields, t == null ? void 0 : t.orderBy, t == null ? void 0 : t.populate, i.tableName, i.viewName]), c.useEffect(() => {
    const e = u != null && u.results ? u == null ? void 0 : u.results.data.map((a) => ({
      ...a,
      isCreated: !0
    })) : [];
    e.length > h - 1 && e.pop(), F(e), L(B);
  }, [u, B]), c.useEffect(() => {
    if (t != null && t.fields) {
      const e = q.filter((s) => l.find((d) => d.accessorKey === s)).map((s) => l.find((d) => d.accessorKey === s)), a = E.differenceBy(l, e, "accessorKey");
      a.splice(1, 0, e), R(a.flat().map((s) => ({
        ...s,
        isShown: !!q.find((d) => d === s.accessorKey || s.accessorKey === "rowId")
      })));
    } else
      R(l.map((e) => ({
        ...e,
        isShown: !["id", "updated_at", "created_at"].includes(e.accessorKey)
      })));
  }, [l, q, t == null ? void 0 : t.fields]);
  const b = c.useMemo(() => [f, K].flat(), [f, K]), A = c.useMemo(() => S.filter((e) => e.isShown === !0 && e.accessorKey), [S]);
  return /* @__PURE__ */ U(X, {
    columns: H(A),
    data: b,
    tableName: r == null ? void 0 : r.name,
    variant: "showFooterWithOperationTable",
    defaultColumn: J
  });
}
export {
  ee as default,
  Q as disableUpdateField
};
