var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/PisellGroupView/PisellGroupView.tsx
var PisellGroupView_exports = {};
__export(PisellGroupView_exports, {
  PisellGroupView: () => MemoizedPisellGroupView,
  default: () => PisellGroupView_default
});
module.exports = __toCommonJS(PisellGroupView_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_PisellBasicGrid = __toESM(require("../PisellBasicGrid"));
var import_utils2 = require("./utils");
var import_PisellGroupView = require("./PisellGroupView.less");
var mapBasicGridDirection = (direction) => direction === "vertical" ? "vertical" : "horizontal";
var PisellGroupView = (props) => {
  const {
    dataSource,
    groupBy,
    getGroupId,
    sortGroups,
    sortItems,
    renderHeader,
    renderStats,
    renderItem,
    renderActions,
    itemLayout = "stack",
    renderBody,
    renderGroup,
    renderEmpty,
    rowKey,
    variant = "desktop",
    className,
    style
  } = props;
  const groupFieldNames = (0, import_react.useMemo)(
    () => (0, import_utils2.getGroupFieldNames)(groupBy),
    [groupBy]
  );
  const groups = (0, import_react.useMemo)(
    () => (0, import_utils.groupBy)(dataSource || [], groupBy, {
      getGroupId,
      sortGroups,
      sortItems
    }),
    [dataSource, groupBy, getGroupId, sortGroups, sortItems]
  );
  const renderDefaultBody = (group) => {
    if (!renderItem) {
      return null;
    }
    const itemNodes = group.items.map((item, index) => /* @__PURE__ */ import_react.default.createElement(import_react.Fragment, { key: (0, import_utils2.resolveItemKey)(item, index, rowKey) }, renderItem(item, {
      group,
      index,
      groupKeys: groupFieldNames
    })));
    if (itemLayout === "stack") {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_utils2.COMPONENT_PREFIX}__body-stack` }, itemNodes);
    }
    const layout = itemLayout;
    return /* @__PURE__ */ import_react.default.createElement(
      import_PisellBasicGrid.default,
      {
        direction: mapBasicGridDirection(layout.direction),
        wrap: layout.wrap ?? "wrap",
        columns: layout.columns,
        gap: layout.gap
      },
      itemNodes
    );
  };
  const renderSingleGroup = (group) => {
    const headerNode = (renderHeader == null ? void 0 : renderHeader(group)) ?? null;
    const statsNode = (renderStats == null ? void 0 : renderStats(group)) ?? null;
    const defaultBody = renderDefaultBody(group);
    const bodyNode = renderBody ? renderBody(group, defaultBody) : defaultBody;
    const actionsNode = (renderActions == null ? void 0 : renderActions(group)) ?? null;
    if (renderGroup) {
      const blocks = {
        header: headerNode,
        stats: statsNode,
        body: bodyNode,
        actions: actionsNode
      };
      return /* @__PURE__ */ import_react.default.createElement(import_react.Fragment, { key: group.groupId }, renderGroup(group, blocks));
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { key: group.groupId, className: `${import_utils2.COMPONENT_PREFIX}__group` }, headerNode !== null && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_utils2.COMPONENT_PREFIX}__header` }, headerNode), statsNode !== null && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_utils2.COMPONENT_PREFIX}__stats` }, statsNode), bodyNode !== null && bodyNode !== void 0 && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_utils2.COMPONENT_PREFIX}__body` }, bodyNode), actionsNode !== null && /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_utils2.COMPONENT_PREFIX}__actions` }, actionsNode));
  };
  const isEmpty = groups.length === 0;
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(
        import_utils2.COMPONENT_PREFIX,
        `${import_utils2.COMPONENT_PREFIX}--${variant}`,
        className
      ),
      style
    },
    isEmpty ? (renderEmpty == null ? void 0 : renderEmpty()) ?? null : groups.map(renderSingleGroup)
  );
};
var MemoizedPisellGroupView = (0, import_react.memo)(
  PisellGroupView
);
var PisellGroupView_default = MemoizedPisellGroupView;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PisellGroupView
});
