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/drag-sort-tree/TreeItem/index.tsx
var TreeItem_exports = {};
__export(TreeItem_exports, {
  default: () => TreeItem_default
});
module.exports = __toCommonJS(TreeItem_exports);
var import_react = __toESM(require("react"));
var import_sortable = require("@dnd-kit/sortable");
var import_core = require("@dnd-kit/core");
var import_antd = require("antd");
var import_DotsSix = __toESM(require("@pisell/icon/es/DotsSix"));
var import_locales = require("../../../locales");
var import_utilities = require("@dnd-kit/utilities");
var import_icons = require("@ant-design/icons");
var import_sortable2 = require("@dnd-kit/sortable");
var import_modifiers = require("@dnd-kit/modifiers");
var import_classnames = __toESM(require("classnames"));
var import_index = require("./index.less");
var prefix = "pisell-lowcode";
var TreeItem = (props) => {
  const {
    id,
    item,
    depth,
    sensors,
    childrenProps,
    lists,
    expandedKeys,
    onChange
  } = props;
  const {
    attributes,
    listeners,
    setNodeRef,
    transform,
    transition,
    isDragging
  } = (0, import_sortable.useSortable)({ id });
  const [isMobile, setIsMobile] = (0, import_react.useState)(window.innerWidth < 600);
  const style = {
    transform: import_utilities.CSS.Transform.toString(transform && { ...transform, scaleY: 1 }),
    transition
  };
  const rowKey = (0, import_react.useMemo)(() => {
    return (childrenProps == null ? void 0 : childrenProps.rowKey) || "id";
  }, [childrenProps == null ? void 0 : childrenProps.rowKey]);
  const handleDragEnd = (event) => {
    var _a, _b, _c;
    const { active, over } = event;
    if (!over || !active || !lists.length)
      return;
    try {
      let [activeParentId, activeId] = (_a = active == null ? void 0 : active.id) == null ? void 0 : _a.split("-");
      let [overParentId, overId] = (_b = over == null ? void 0 : over.id) == null ? void 0 : _b.split("-");
      const parentIndex = lists.findIndex(
        (item2) => item2[rowKey] == activeParentId
      );
      if (activeId !== overId && activeParentId === overParentId && parentIndex !== -1) {
        const activeIndex = item.children.findIndex(
          (item2) => item2[rowKey] == activeId
        );
        const overIndex = item.children.findIndex(
          (item2) => item2[rowKey] == overId
        );
        if (activeIndex !== -1 && overIndex !== -1) {
          const newChildren = (0, import_sortable2.arrayMove)(
            (_c = lists[parentIndex]) == null ? void 0 : _c.children,
            activeIndex,
            overIndex
          );
          const _lists = [
            ...lists.slice(0, parentIndex),
            { ...lists == null ? void 0 : lists[parentIndex], children: newChildren },
            ...lists.slice(parentIndex + 1)
          ];
          onChange && onChange([..._lists]);
        }
      }
    } catch (error) {
    }
  };
  const handleShow = (e) => {
    e.stopPropagation();
    e.preventDefault();
    const index = lists == null ? void 0 : lists.findIndex(
      (node) => node[rowKey] === item[rowKey]
    );
    if (index !== -1) {
      lists[index].show = !lists[index].show;
      onChange == null ? void 0 : onChange([...lists]);
    }
  };
  const onSelect = () => {
    var _a;
    if (depth === 0 && ((_a = item.children) == null ? void 0 : _a.length)) {
      const key = item[rowKey];
      (childrenProps == null ? void 0 : childrenProps.onHiddenChange) && (childrenProps == null ? void 0 : childrenProps.onHiddenChange(key));
    }
  };
  const renderChildren = (0, import_react.useMemo)(() => {
    var _a;
    const showChildren = ((_a = item == null ? void 0 : item.children) == null ? void 0 : _a.length) > 0 && (expandedKeys == null ? void 0 : expandedKeys.includes(item[rowKey]));
    const _isAvailableTab = (childrenProps == null ? void 0 : childrenProps.tabKey) === "available";
    return showChildren && /* @__PURE__ */ import_react.default.createElement(
      import_sortable2.SortableContext,
      {
        items: item.children.map(
          (child) => `${item[rowKey]}-${child[rowKey]}`
        ),
        strategy: import_sortable2.verticalListSortingStrategy
      },
      item.children.map(
        (child) => _isAvailableTab && (child == null ? void 0 : child.is_available) || !_isAvailableTab ? /* @__PURE__ */ import_react.default.createElement(
          TreeItem,
          {
            id: `${item[rowKey]}-${child[rowKey]}`,
            key: `${item[rowKey]}-${child[rowKey]}`,
            item: child,
            lists,
            depth: depth + 1,
            childrenProps
          }
        ) : null
      )
    );
  }, [
    item.children,
    expandedKeys,
    rowKey,
    lists,
    childrenProps == null ? void 0 : childrenProps.tabKey,
    isDragging
  ]);
  const rightIcon = (0, import_react.useMemo)(() => {
    const hiddenRightIcon = childrenProps == null ? void 0 : childrenProps.hiddenRightIcon;
    return !hiddenRightIcon ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)(`${prefix}-drag-sort-tree-icon`),
        onClick: handleShow
      },
      item.show ? /* @__PURE__ */ import_react.default.createElement(import_icons.EyeOutlined, null) : /* @__PURE__ */ import_react.default.createElement(import_icons.EyeInvisibleOutlined, null)
    ) : null;
  }, [childrenProps == null ? void 0 : childrenProps.hiddenRightIcon, item == null ? void 0 : item.show, lists]);
  const warningIcon = (0, import_react.useMemo)(() => {
    const hiddenWarningIcon = childrenProps == null ? void 0 : childrenProps.hiddenWarningIcon;
    return !hiddenWarningIcon && !(item == null ? void 0 : item.is_available) ? /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(`${prefix}-drag-sort-tree-item-icon`) }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Tooltip,
      {
        title: (0, import_locales.getText)("drag-sort-tree-unavailable-today-tip"),
        color: "#ffffff",
        overlayInnerStyle: { color: "#344054" }
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.WarningOutlined, { style: { color: "#FF2020" } })
    )) : null;
  }, [childrenProps == null ? void 0 : childrenProps.hiddenWarningIcon, item == null ? void 0 : item.is_available]);
  const handleWindowResize = () => {
    let windowWidth = window.innerWidth;
    setIsMobile(windowWidth < 600);
  };
  (0, import_react.useEffect)(() => {
    window.addEventListener("resize", handleWindowResize);
    return () => {
      window.removeEventListener("resize", handleWindowResize);
    };
  }, []);
  return /* @__PURE__ */ import_react.default.createElement(
    import_core.DndContext,
    {
      sensors,
      modifiers: [import_modifiers.restrictToVerticalAxis, import_modifiers.restrictToParentElement],
      onDragEnd: handleDragEnd
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: setNodeRef,
        style,
        ...attributes,
        ...!isMobile ? listeners : {}
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: (0, import_classnames.default)(
            `${prefix}-drag-sort-tree`,
            childrenProps == null ? void 0 : childrenProps.className,
            isMobile ? `${prefix}-drag-sort-phone-tree` : ""
          ),
          style: {
            paddingLeft: !depth ? "10px" : `${depth * 40}px`,
            cursor: isDragging ? "grabbing" : "grab"
          },
          onClick: onSelect
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(`${prefix}-drag-sort-tree-item`) }, !(childrenProps == null ? void 0 : childrenProps.hiddenDraggableIcon) ? /* @__PURE__ */ import_react.default.createElement(
          import_DotsSix.default,
          {
            ...isMobile ? listeners : {},
            className: (0, import_classnames.default)(`${prefix}-drag-sort-tree-item-dot-icon`),
            style: {
              left: isMobile ? !depth ? "-14px" : "12px" : 0
            }
          }
        ) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: `${prefix}-drag-sort-tree-title` }, (childrenProps == null ? void 0 : childrenProps.titleRender) || (item == null ? void 0 : item.title))),
        !depth ? rightIcon : warningIcon
      ),
      renderChildren
    )
  );
};
var TreeItem_default = TreeItem;
