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