"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);

// packages/block-editor/src/components/list-view/drop-indicator.js
var drop_indicator_exports = {};
__export(drop_indicator_exports, {
  default: () => ListViewDropIndicatorPreview
});
module.exports = __toCommonJS(drop_indicator_exports);
var import_clsx = __toESM(require("clsx"));
var import_components = require("@wordpress/components");
var import_dom = require("@wordpress/dom");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_block_icon = __toESM(require("../block-icon"));
var import_use_block_display_information = __toESM(require("../use-block-display-information"));
var import_use_block_display_title = __toESM(require("../block-title/use-block-display-title"));
var import_expander = __toESM(require("./expander"));
var import_jsx_runtime = require("react/jsx-runtime");
function ListViewDropIndicatorPreview({
  draggedBlockClientId,
  listViewRef,
  blockDropTarget
}) {
  const blockInformation = (0, import_use_block_display_information.default)(draggedBlockClientId);
  const blockTitle = (0, import_use_block_display_title.default)({
    clientId: draggedBlockClientId,
    context: "list-view"
  });
  const { rootClientId, clientId, dropPosition } = blockDropTarget || {};
  const [rootBlockElement, blockElement] = (0, import_element.useMemo)(() => {
    if (!listViewRef.current) {
      return [];
    }
    const _rootBlockElement = rootClientId ? listViewRef.current.querySelector(
      `[data-block="${rootClientId}"]`
    ) : void 0;
    const _blockElement = clientId ? listViewRef.current.querySelector(
      `[data-block="${clientId}"]`
    ) : void 0;
    return [_rootBlockElement, _blockElement];
  }, [listViewRef, rootClientId, clientId]);
  const targetElement = blockElement || rootBlockElement;
  const rtl = (0, import_i18n.isRTL)();
  const getDropIndicatorWidth = (0, import_element.useCallback)(
    (targetElementRect, indent) => {
      if (!targetElement) {
        return 0;
      }
      let width = targetElement.offsetWidth;
      const scrollContainer = (0, import_dom.getScrollContainer)(
        targetElement,
        "horizontal"
      );
      const ownerDocument = targetElement.ownerDocument;
      const windowScroll = scrollContainer === ownerDocument.body || scrollContainer === ownerDocument.documentElement;
      if (scrollContainer && !windowScroll) {
        const scrollContainerRect = scrollContainer.getBoundingClientRect();
        const distanceBetweenContainerAndTarget = (0, import_i18n.isRTL)() ? scrollContainerRect.right - targetElementRect.right : targetElementRect.left - scrollContainerRect.left;
        const scrollContainerWidth = scrollContainer.clientWidth;
        if (scrollContainerWidth < width + distanceBetweenContainerAndTarget) {
          width = scrollContainerWidth - distanceBetweenContainerAndTarget;
        }
        if (!rtl && targetElementRect.left + indent < scrollContainerRect.left) {
          width -= scrollContainerRect.left - targetElementRect.left;
          return width;
        }
        if (rtl && targetElementRect.right - indent > scrollContainerRect.right) {
          width -= targetElementRect.right - scrollContainerRect.right;
          return width;
        }
      }
      return width - indent;
    },
    [rtl, targetElement]
  );
  const style = (0, import_element.useMemo)(() => {
    if (!targetElement) {
      return {};
    }
    const targetElementRect = targetElement.getBoundingClientRect();
    return {
      width: getDropIndicatorWidth(targetElementRect, 0)
    };
  }, [getDropIndicatorWidth, targetElement]);
  const horizontalScrollOffsetStyle = (0, import_element.useMemo)(() => {
    if (!targetElement) {
      return {};
    }
    const scrollContainer = (0, import_dom.getScrollContainer)(targetElement);
    const ownerDocument = targetElement.ownerDocument;
    const windowScroll = scrollContainer === ownerDocument.body || scrollContainer === ownerDocument.documentElement;
    if (scrollContainer && !windowScroll) {
      const scrollContainerRect = scrollContainer.getBoundingClientRect();
      const targetElementRect = targetElement.getBoundingClientRect();
      const distanceBetweenContainerAndTarget = rtl ? scrollContainerRect.right - targetElementRect.right : targetElementRect.left - scrollContainerRect.left;
      if (!rtl && scrollContainerRect.left > targetElementRect.left) {
        return {
          transform: `translateX( ${distanceBetweenContainerAndTarget}px )`
        };
      }
      if (rtl && scrollContainerRect.right < targetElementRect.right) {
        return {
          transform: `translateX( ${distanceBetweenContainerAndTarget * -1}px )`
        };
      }
    }
    return {};
  }, [rtl, targetElement]);
  const ariaLevel = (0, import_element.useMemo)(() => {
    if (!rootBlockElement) {
      return 1;
    }
    const _ariaLevel = parseInt(
      rootBlockElement.getAttribute("aria-level"),
      10
    );
    return _ariaLevel ? _ariaLevel + 1 : 1;
  }, [rootBlockElement]);
  const hasAdjacentSelectedBranch = (0, import_element.useMemo)(() => {
    if (!targetElement) {
      return false;
    }
    return targetElement.classList.contains("is-branch-selected");
  }, [targetElement]);
  const popoverAnchor = (0, import_element.useMemo)(() => {
    const isValidDropPosition = dropPosition === "top" || dropPosition === "bottom" || dropPosition === "inside";
    if (!targetElement || !isValidDropPosition) {
      return void 0;
    }
    return {
      contextElement: targetElement,
      getBoundingClientRect() {
        const rect = targetElement.getBoundingClientRect();
        let left = rect.left;
        let top = 0;
        const scrollContainer = (0, import_dom.getScrollContainer)(
          targetElement,
          "horizontal"
        );
        const doc = targetElement.ownerDocument;
        const windowScroll = scrollContainer === doc.body || scrollContainer === doc.documentElement;
        if (scrollContainer && !windowScroll) {
          const scrollContainerRect = scrollContainer.getBoundingClientRect();
          const scrollbarWidth = rtl ? scrollContainer.offsetWidth - scrollContainer.clientWidth : 0;
          if (left < scrollContainerRect.left + scrollbarWidth) {
            left = scrollContainerRect.left + scrollbarWidth;
          }
        }
        if (dropPosition === "top") {
          top = rect.top - rect.height * 2;
        } else {
          top = rect.top;
        }
        const width = getDropIndicatorWidth(rect, 0);
        const height = rect.height;
        return new window.DOMRect(left, top, width, height);
      }
    };
  }, [targetElement, dropPosition, getDropIndicatorWidth, rtl]);
  if (!targetElement) {
    return null;
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Popover,
    {
      animate: false,
      anchor: popoverAnchor,
      focusOnMount: false,
      className: "block-editor-list-view-drop-indicator--preview",
      variant: "unstyled",
      flip: false,
      resize: true,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        "div",
        {
          style,
          className: (0, import_clsx.default)(
            "block-editor-list-view-drop-indicator__line",
            {
              "block-editor-list-view-drop-indicator__line--darker": hasAdjacentSelectedBranch
            }
          ),
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
            "div",
            {
              className: "block-editor-list-view-leaf",
              "aria-level": ariaLevel,
              children: [
                /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
                  "div",
                  {
                    className: (0, import_clsx.default)(
                      "block-editor-list-view-block-select-button",
                      "block-editor-list-view-block-contents"
                    ),
                    style: horizontalScrollOffsetStyle,
                    children: [
                      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_expander.default, { onClick: () => {
                      } }),
                      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                        import_block_icon.default,
                        {
                          icon: blockInformation?.icon,
                          showColors: true,
                          context: "list-view"
                        }
                      ),
                      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                        import_components.__experimentalHStack,
                        {
                          alignment: "center",
                          className: "block-editor-list-view-block-select-button__label-wrapper",
                          justify: "flex-start",
                          spacing: 1,
                          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "block-editor-list-view-block-select-button__title", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalTruncate, { ellipsizeMode: "auto", children: blockTitle }) })
                        }
                      )
                    ]
                  }
                ),
                /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-list-view-block__menu-cell" })
              ]
            }
          )
        }
      )
    }
  );
}
//# sourceMappingURL=drop-indicator.js.map
