import { IconType } from '@src/types/icons';

import { type LibrariesTreeItem } from './types';
import { type SidebarModel, type SidebarNode } from './types/model';

const ICON_EMOJI_VALUE = {
  custom: true,
  type: IconType.EMOJI,
  value: '😆',
};

const ICON_IMAGE_VALUE = {
  custom: true,
  type: IconType.IMAGE,
  value: 'https://hu-multimedia-dev.s3.amazonaws.com/images/12-Cup.png',
};

/**
 * Full flat tree: root ids match rootItemsMock (5255, 6466) so drilling from
 * root list shows their children when the story switches to this mock.
 */
export const articleItemsMock: LibrariesTreeItem[] = [
  {
    icon: ICON_IMAGE_VALUE,
    id: 5255,
    isActive: true,
    title: 'Hijo 1',
    showBadge: true,
    parentId: null,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_EMOJI_VALUE,
    id: 6466,
    isActive: true,
    title: 'Hijo 2',
    parentId: null,
    position: 1,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 5,
    isActive: false,
    title: 'Hijo 1.1',
    showBadge: true,
    parentId: 5255,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 15,
    isActive: false,
    title: 'Hijo 1.2',
    parentId: 5255,
    position: 1,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 16,
    isActive: false,
    title: 'Hijo 1.3',
    parentId: 5255,
    position: 2,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 17,
    isActive: false,
    title: 'Hijo 1.4',
    parentId: 5255,
    position: 3,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 6,
    isActive: false,
    title: 'Hijo 1.1.1',
    parentId: 5,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 10,
    isActive: false,
    title: 'Hijo 1.1.2',
    parentId: 5,
    position: 1,
    hasChildren: false,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 7,
    isActive: false,
    title: 'Hijo 1.1.1.1',
    parentId: 6,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 8,
    isActive: false,
    title: 'Hijo 1.1.1.1.1',
    parentId: 7,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 9,
    isActive: false,
    title: 'Hijo 1.1.1.1.1.1',
    parentId: 8,
    position: 0,
    hasChildren: false,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 113,
    isActive: false,
    title: 'Hijo 1.2.1',
    parentId: 15,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 11,
    isActive: false,
    title: 'Hijo 1.2.2',
    parentId: 15,
    position: 1,
    hasChildren: false,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 12,
    isActive: false,
    title: 'Hijo 1.2.1.1',
    parentId: 113,
    position: 0,
    hasChildren: false,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 13,
    isActive: false,
    title: 'Hijo 1.2.1.2',
    parentId: 113,
    position: 1,
    hasChildren: false,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 14,
    isActive: false,
    title: 'Hijo 1.2.1.3',
    parentId: 113,
    position: 2,
    hasChildren: false,
  },
  {
    icon: ICON_EMOJI_VALUE,
    id: 3,
    isActive: false,
    title: 'Hijo 2.1',
    parentId: 6466,
    position: 0,
    hasChildren: false,
  },
  {
    icon: ICON_EMOJI_VALUE,
    id: 4,
    isActive: false,
    title: 'Hijo 2.2',
    parentId: 6466,
    position: 1,
    hasChildren: false,
  },
];

export const rootItemsMock: LibrariesTreeItem[] = [
  {
    icon: ICON_IMAGE_VALUE,
    id: 5255,
    isActive: true,
    showBadge: true,
    title:
      'This is a really long title that should be truncated This is a really long title that should be truncated This is a really long title that should be truncated',
    parentId: null,
    position: 0,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 6466,
    isActive: false,
    showBadge: true,
    title: 'Hijo 2',
    parentId: null,
    position: 1,
    hasChildren: true,
  },
  {
    icon: ICON_IMAGE_VALUE,
    id: 7477,
    isActive: true,
    title: 'Hijo 3',
    parentId: null,
    position: 2,
    hasChildren: false,
  },
];

const buildModelFromFlatItems = (
  items: LibrariesTreeItem[],
  headerTitle: string,
): SidebarModel => {
  const byId = new Map(items.map(item => [item.id, item]));
  const childCounts = new Map<number, number>();

  for (const item of items) {
    if (item.parentId != null) {
      childCounts.set(item.parentId, (childCounts.get(item.parentId) ?? 0) + 1);
    }
  }

  const getDepth = (id: number): number => {
    const item = byId.get(id);
    if (!item || item.parentId == null) return 1;
    return getDepth(item.parentId) + 1;
  };

  const nodes: SidebarNode[] = items.map(item => ({
    id: item.id,
    parentId: item.parentId,
    position: item.position ?? 0,
    title: item.title,
    icon: item.icon,
    status: item.isActive ? 'enabled' : 'disabled',
    childCount: childCounts.get(item.id) ?? 0,
    depth: getDepth(item.id),
  }));

  return { headerTitle, nodes };
};

export const rootModelMock = buildModelFromFlatItems(rootItemsMock, '');

export const articleModelMock = buildModelFromFlatItems(
  articleItemsMock,
  'Hijo 1',
);

export const defaultCapabilitiesMock = {
  canCreate: true,
  showStatusBadge: true,
} as const;
