"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FileTree = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _useTreeNavigation = require("../hooks/use-tree-navigation");
var _fileTreeAdapter = require("./file-tree-adapter");
var _fileTreeNavigationAdapter = require("./file-tree-navigation-adapter");
var _ = require(".");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/** Displays a browsable file tree navigation component
    @midnight-gray-supported
    @alpine-light-supported
 */
const FileTree = ({
  fileTree = [],
  folderComponentProps,
  fileComponentProps,
  rootClass,
  fileTestId,
  folderTestId,
  rowSize,
  FileComponent = _.FileTreeFileComponent,
  FolderComponent = _.FileTreeFolderComponent,
  ItemContentComponent = _.FileTreeItemContent,
  rootTestId = 'file-tree-root',
  branchTestId = 'file-tree-branch',
  itemTestId = 'file-tree-item-wrapper',
  disabled = false,
  sortingStrategy = value => value,
  isLoading = false,
  loadingPattern = [0, 1, 1, 2, 0]
}) => {
  const adapter = (0, _react.useMemo)(() => (0, _fileTreeNavigationAdapter.getFileTreeNavigationAdapter)({
    isExpandedByDefault: !!folderComponentProps?.expand
  }), [!!folderComponentProps?.expand]);
  if (isLoading) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_.FileTreeLoading, {
      loadingPattern: loadingPattern,
      rootClass: rootClass,
      rootTestId: rootTestId,
      branchTestId: branchTestId
    });
  }
  if (!fileTree.length) {
    return null;
  }
  const baseLevel = 0;
  const renderFileComponent = item => /*#__PURE__*/(0, _jsxRuntime.jsx)(FileComponent, {
    testId: fileTestId,
    item: item,
    itemTestId: itemTestId,
    disabled: disabled,
    ItemContentComponent: ItemContentComponent,
    rowSize: rowSize,
    ...fileComponentProps
  }, item.name);
  const renderFolderComponent = (item, currentLevel) => /*#__PURE__*/(0, _jsxRuntime.jsx)(FolderComponent, {
    testId: folderTestId,
    itemTestId: itemTestId,
    item: item,
    disabled: disabled,
    ItemContentComponent: ItemContentComponent,
    renderBranch: content => renderBranch(content, currentLevel),
    rowSize: rowSize,
    ...folderComponentProps
  }, item.name);
  function renderBranch(content, level) {
    const currentLevel = level + 1;
    const role = level === 0 ? 'tree' : 'group';
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
      className: "branch",
      "test-id": `${branchTestId}-${currentLevel}`,
      role: role,
      children: content.map(contentEntry => {
        const {
          type
        } = contentEntry;
        if (type === _fileTreeAdapter.FILE_TYPE.FOLDER) {
          return renderFolderComponent(contentEntry, currentLevel);
        }
        if (type === _fileTreeAdapter.FILE_TYPE.FILE) {
          return renderFileComponent(contentEntry);
        }
        return null;
      })
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_useTreeNavigation.TreeNavigationProvider, {
    adapter: adapter,
    tree: fileTree,
    isDisabled: disabled,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)('file-tree-root', rootClass),
      "test-id": rootTestId,
      children: renderBranch(sortingStrategy(fileTree), baseLevel)
    })
  });
};
exports.FileTree = FileTree;