"use strict";
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);
var collapsible_list_item_exports = {};
__export(collapsible_list_item_exports, {
  default: () => collapsible_list_item_default
});
module.exports = __toCommonJS(collapsible_list_item_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_data_hooks = require("../data-hooks.js");
var import_icons = require("../icons/index.js");
var import_collapsible_list_component_scss = __toESM(require("../statics/styles/collapsible-list-component-scss.js"));
var import_collapsible_list_pair_scss = __toESM(require("../statics/styles/collapsible-list-pair-scss.js"));
var import_collapsible_list_context = require("./collapsible-list-context.js");
const CollapsibleListItem = ({ node, RicosNodesRenderer }) => {
  const { isExpanded: isItemExpanded, toggle } = (0, import_react.useContext)(import_collapsible_list_context.CollapsibleListContext);
  const { tabIndex } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const isExpanded = isItemExpanded(node.id);
  if (!node.nodes || node.nodes.length === 0) {
    return null;
  }
  const [firstChild, ...restOfChildren] = node.nodes ?? [];
  const bodyId = `collapsible-list-body-${node.id}`;
  const handleKeyDown = (event) => {
    if (event.key === " ") {
      event.preventDefault();
    }
  };
  const handleKeyUp = (event) => {
    if (event.key === " " || event.key === "Enter") {
      toggle(node.id);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { "data-hook": `${import_data_hooks.COLLAPSIBLE_LIST_ITEM} ${isExpanded ? "expanded" : "collapsed"}` }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(import_collapsible_list_pair_scss.default.itemContainer, import_collapsible_list_pair_scss.default.itemContainerViewer) }, /* @__PURE__ */ import_react.default.createElement("div", { className: import_collapsible_list_pair_scss.default.item }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(import_collapsible_list_pair_scss.default.itemHeader, "has-custom-focus"),
      role: "button",
      tabIndex,
      onClick: () => toggle(node.id),
      onKeyDown: handleKeyDown,
      onKeyUp: handleKeyUp,
      "aria-expanded": isExpanded,
      "aria-controls": bodyId
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_collapsible_list_pair_scss.default.expandIcon }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: import_collapsible_list_component_scss.default.iconContainer,
        "data-hook": import_data_hooks.EXPAND_BUTTON,
        "aria-hidden": "true"
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_icons.ArrowIcon,
        {
          className: (0, import_classnames.default)(import_collapsible_list_component_scss.default.icon, {
            [import_collapsible_list_component_scss.default.isExpanded]: isExpanded
          })
        }
      )
    )),
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_collapsible_list_pair_scss.default.item }, /* @__PURE__ */ import_react.default.createElement(RicosNodesRenderer, { nodes: [firstChild] }))
  ), /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      id: bodyId,
      className: (0, import_classnames.default)(
        import_collapsible_list_pair_scss.default.itemBody,
        isExpanded ? void 0 : import_collapsible_list_pair_scss.default.childrenHidden
      )
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_collapsible_list_pair_scss.default.expandIconPlaceholder }),
    /* @__PURE__ */ import_react.default.createElement("div", { className: import_collapsible_list_pair_scss.default.item }, /* @__PURE__ */ import_react.default.createElement(RicosNodesRenderer, { nodes: restOfChildren }))
  )))));
};
const CollapsibleListItemRenderer = ({ node, ...props }) => {
  return /* @__PURE__ */ import_react.default.createElement(CollapsibleListItem, { key: node.id, node, ...props });
};
var collapsible_list_item_default = CollapsibleListItemRenderer;
//# sourceMappingURL=collapsible-list-item.js.map
