import c from "react";
import { G as m, t as O, u as A, w as G, v as j, av as k, x as F, z as U, y as H, m as J, E as Q, F as X } from "./App-4f8b3c63.mjs";
import "@emotion/react";
import "react-dom";
import "@emotion/styled";
import "@feedloop/formula-editor";
import "@feedloop/expression-parser";
const Y = /* @__PURE__ */ new Set(["id", "created_at", "updated_at", "auth_provider"]), Z = c.lazy(() => import("./App-4f8b3c63.mjs").then((n) => n.bl)), $ = (n) => {
  var d;
  if (!n.query)
    return;
  const p = 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 && (p[r] = {
      default: "",
      type: ""
    });
  }), p;
};
function ae() {
  const n = m.useSelectState((e) => e.sortList), p = m.useSelectState((e) => e.filterList), y = O(), f = A(), d = G(), {
    table: r,
    grid: o
  } = j(), D = m.useSelectState((e) => e.rowsContext), K = m.useSelectState((e) => e.columnsContext), {
    limit: S,
    offset: h,
    setIsScrollTable: _
  } = 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: M,
    addFilterList: T,
    addSortList: V,
    setViewFields: W
  } = m.useActions(), q = c.useMemo(() => F.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: u,
    mutate: P,
    isValidating: E
  } = U([y, n, p, S, h, q, 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: p,
        limit: S,
        offset: h,
        params: q,
        table: r == null ? void 0 : r.name,
        view: o == null ? void 0 : o.name
      } : {
        name: "data",
        orderBy: n,
        condition: p,
        limit: S,
        offset: h,
        params: q,
        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(E);
  }, [E]), c.useEffect(() => {
    let e = [];
    return d.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: 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
    })))), 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, d.tableName, d.viewName]), c.useEffect(() => {
    const e = u != null && u.results ? u == null ? void 0 : u.results.data.map((a) => ({
      ...a,
      isCreated: !0
    })) : [];
    e.length > S - 1 && e.pop(), I(e), L(P);
  }, [u, 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 = F.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(() => [D, N].flat(), [D, N]), z = 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(z),
    data: w,
    tableName: r == null ? void 0 : r.name,
    variant: "showFooterWithOperationTable",
    defaultColumn: X
  });
}
export {
  ae as default,
  Y as disableUpdateField
};
