import type { PlanFilterStatus, PlanQuickFilter } from '../../../features/planBoard';
import type { PlanSidebarKey, PlaySidebarKey } from '../types';

export const PLAN_GROUP_LABEL = '작업';

export const PLAN_FILTER_LABELS: Record<PlanFilterStatus, string> = {
  all: '자동화 현황',
  'in-progress': '실행 중 (0)',
  done: '완료',
  error: '실패 (0)',
};

export const DOCS_DEFAULT_FOLDER = 'project';

export const DOCS_FOLDER_LABELS: Record<string, string> = {
  project: '프로젝트 구조',
};

export const PLAN_SIDEBAR_LABELS: Record<PlanSidebarKey, string> = {
  ...PLAN_FILTER_LABELS,
  release: 'release (0)',
  'release-review': 'release 검수',
  board: '작업 요청',
  charts: '차트',
  schedule: '스케줄',
  history: '이력',
  'automation-type': '자동화 유형',
  'automation-context': 'Context 유형',
  'token-setting': '설정',
  'shared-resource': '공유 리소스 관리',
  'server-command': 'Command',
};

export const PLAN_BASE_OPEN_KEYS = ['plan-group', 'token-management-group', 'server-group', 'chat-group', 'app-log-group'] as const;
export const PLAY_BASE_OPEN_KEYS = ['play-group', 'play-layout-group', 'play-apps-group', 'play-apps-general-group'] as const;
export const PLAY_LAYOUT_RECORD_PREFIX = 'layout-record:' as const;
export const PLAN_MENU_STATUS_ORDER: PlanFilterStatus[] = ['all', 'in-progress', 'error', 'done'];

export const PLAY_SIDEBAR_LABELS: Record<Extract<PlaySidebarKey, 'layout' | 'draw' | 'apps' | 'test' | 'cbt'>, string> = {
  layout: 'Layout Editor',
  draw: 'Layout Draw',
  apps: 'Apps',
  test: 'Test App',
  cbt: 'CBT',
};

export const PLAN_MENU_ANCHOR_IDS: Partial<Record<PlanSidebarKey, string>> = {
  all: 'plan-menu-all',
  'in-progress': 'plan-menu-in-progress',
  release: 'plan-menu-release',
  'release-review': 'plan-menu-release-review',
  done: 'plan-menu-done',
  error: 'plan-menu-error',
  board: 'plan-menu-board',
  charts: 'plan-menu-charts',
  schedule: 'plan-menu-schedule',
  history: 'plan-menu-history',
  'automation-type': 'plan-menu-automation-type',
  'automation-context': 'plan-menu-automation-context',
  'token-setting': 'plan-menu-token-setting',
  'shared-resource': 'plan-menu-shared-resource',
  'server-command': 'plan-menu-server-command',
};

export function resolvePlanOpenKeys() {
  return [...PLAN_BASE_OPEN_KEYS];
}

export function resolvePlayOpenKeys() {
  return [...PLAY_BASE_OPEN_KEYS];
}

export function resolveSavedLayoutMenuKey(layoutId: string): PlaySidebarKey {
  return `${PLAY_LAYOUT_RECORD_PREFIX}${layoutId}`;
}

export function resolveSavedLayoutIdFromMenuKey(key: PlaySidebarKey) {
  return key.startsWith(PLAY_LAYOUT_RECORD_PREFIX) ? key.slice(PLAY_LAYOUT_RECORD_PREFIX.length) : null;
}

export function resolvePlaySidebarLabel(selectedPlayMenu: PlaySidebarKey, savedLayouts: Array<{ id: string; name: string }>) {
  const savedLayoutId = resolveSavedLayoutIdFromMenuKey(selectedPlayMenu);

  if (
    selectedPlayMenu === 'layout' ||
    selectedPlayMenu === 'draw' ||
    selectedPlayMenu === 'apps' ||
    selectedPlayMenu === 'test' ||
    selectedPlayMenu === 'cbt'
  ) {
    return PLAY_SIDEBAR_LABELS[selectedPlayMenu];
  }

  return savedLayouts.find((record) => record.id === savedLayoutId)?.name ?? 'Saved Layout';
}

export function renderPlanMenuLabel(menu: PlanSidebarKey, label: string) {
  const anchorId = PLAN_MENU_ANCHOR_IDS[menu];

  if (!anchorId) {
    return label;
  }

  return <span id={anchorId}>{label}</span>;
}

export function resolvePlanQuickFilterMenu(filter: PlanQuickFilter): Extract<PlanSidebarKey, 'in-progress' | 'release' | 'error'> {
  if (filter === 'working') {
    return 'in-progress';
  }

  return filter === 'release-pending-main' ? 'release' : 'error';
}

export function resolvePlanMenuState(menu: PlanSidebarKey) {
  if (menu === 'release') {
    return {
      quickFilter: 'release-pending-main' as PlanQuickFilter,
    };
  }

  return {
    quickFilter: null,
  };
}

export function getDocsSectionLabel(section: string) {
  return DOCS_FOLDER_LABELS[section] ?? section;
}
