import { jsx as t, jsxs as p } from "@emotion/react/jsx-runtime";
import { useState as k, useCallback as I } from "react";
import { CardDetailContainerStyled as $, ContainerLabelValueColumnStyled as F, ContentWrapperColumnStyled as K, LabelHeaderColumnStyled as O, EditorCellWrapperStyled as V, ValueColumnStyled as h, EditablePlaceholderStyled as W, IconButtonEditStyled as j } from "../../../../slots/DataGridSlot.js";
import { DICTIONARY as z } from "../../../../dictionary.js";
import { useModuleDictionary as H } from "@m4l/core";
import { ActionsEditOutlined as P } from "@m4l/icons";
import { useComponentSize as T } from "../../../../../../hooks/useComponentSize/index.js";
function U({
  row: w,
  columns: C,
  onRowsChange: l,
  size: E = "medium",
  rows: o,
  rowIndex: n
}) {
  const [b, c] = k(null), [f, v] = k(0), { getLabel: x } = H(), { currentSize: D } = T(E), i = o[n] ?? w, R = I(() => {
    v((e) => e + 1);
  }, []), L = C.map((e) => e.renderCell ? {
    ...e,
    // Agregar onAfterChange al objeto de la columna
    onAfterChange: R
  } : e);
  return /* @__PURE__ */ t($, { ownerState: {}, children: L.map((e, s) => {
    const r = C[s], g = !!e.onAfterChange ? `${r.key}-${f}` : `${r.key}`;
    return /* @__PURE__ */ t(F, { ownerState: {}, children: /* @__PURE__ */ p(
      K,
      {
        ownerState: {},
        "data-testid": `list-detail-column-${r.key}`,
        onDoubleClick: e.renderEditCell && b !== e.key ? () => c(e.key) : void 0,
        children: [
          /* @__PURE__ */ p(O, { ownerState: {}, variant: "bodyStandard", color: "text.secondary", children: [
            e.name,
            ":"
          ] }),
          (() => {
            if (e.renderEditCell && b === e.key)
              return /* @__PURE__ */ t(V, { ownerState: { size: E }, children: e.renderEditCell({
                column: r,
                row: i,
                rowIdx: s,
                /**
                 * onRowChange es una función que se ejecuta cuando el usuario edita una celda.
                 */
                onRowChange: (d) => {
                  if (l) {
                    const y = o.map(
                      (u, S) => S === n ? d : u
                    );
                    l(y, {
                      indexes: [n],
                      column: r
                    });
                  }
                },
                onClose: () => c(null)
              }) });
            if (e.renderCell) {
              const d = e.renderCell({
                column: e,
                row: i,
                rowIdx: s,
                isCellSelected: !1,
                tabIndex: -1,
                /**
                 * onRowChange es una función que se ejecuta cuando el usuario edita una celda.
                 */
                onRowChange: (y) => {
                  if (l) {
                    const u = o.map(
                      (S, A) => A === n ? y : S
                    );
                    l(u, {
                      indexes: [n],
                      column: r
                    });
                  }
                }
              });
              return /* @__PURE__ */ t(h, { ownerState: {}, variant: "bodyStandard", color: "text.primary", children: d });
            }
            const a = i[e.key], m = a == null || a === "";
            return e.renderEditCell ? /* @__PURE__ */ p(W, { ownerState: {}, children: [
              /* @__PURE__ */ t(
                h,
                {
                  ownerState: {},
                  variant: "bodyStandard",
                  color: m ? "text.secondary" : "text.primary",
                  children: m ? x(z.EDIT_PLACEHOLDER) : a
                }
              ),
              /* @__PURE__ */ t(
                j,
                {
                  ownerState: {},
                  src: /* @__PURE__ */ t(P, {}),
                  onClick: () => c(e.key),
                  size: D,
                  role: "button",
                  "aria-label": "Edit text"
                }
              )
            ] }) : /* @__PURE__ */ t(h, { ownerState: {}, variant: "bodyStandard", color: "text.primary", children: a ?? "-" });
          })()
        ]
      }
    ) }, g);
  }) });
}
export {
  U as ListDetail
};
