import {
  type SidebarNode,
  type SidebarNodeFilterContext,
} from '@material-hu/components/composed-components/LibrariesSidebar/types/model';
import { applySidebarNodeFilter } from '@material-hu/components/composed-components/LibrariesSidebar/utils/adapters';

import { ArticleStatus } from '../types';

const isSidebarNodeVisible = (
  node: SidebarNode,
  canManageArticle: boolean,
  { isRoot }: SidebarNodeFilterContext,
): boolean => {
  if (node.status === ArticleStatus.ENABLED) return true;
  if (isRoot) return Boolean(node.isEditor);
  return canManageArticle;
};

/** Web-only visibility rules for disabled articles in the sidebar. */
export const applySidebarVisibilityFilter = (
  model: Parameters<typeof applySidebarNodeFilter>[0],
  canManageArticle: boolean,
  isRoot: boolean,
) =>
  applySidebarNodeFilter(
    model,
    (node, context) => isSidebarNodeVisible(node, canManageArticle, context),
    { isRoot },
  );
