import { resolveChatWebSocketUrl } from '../../../app/main/mainChatPanel/chatUtils';

export function resolveLayoutConversationTitle(layoutName: string) {
  const normalized = layoutName.trim() || '이름 없는 레이아웃';
  return `${normalized} 명세`;
}

export function resolveLayoutCodexRequestSocketUrl(sessionId: string) {
  const resolvedUrl = new URL(resolveChatWebSocketUrl(sessionId));

  if (typeof window === 'undefined') {
    return resolvedUrl.toString();
  }

  if (['127.0.0.1', 'localhost', '0.0.0.0'].includes(window.location.hostname)) {
    resolvedUrl.protocol = 'wss:';
    resolvedUrl.hostname = 'preview.sm-home.cloud';
    resolvedUrl.port = '';
    resolvedUrl.pathname = '/ws/chat';
  }

  return resolvedUrl.toString();
}

export function isReusableLayoutConversation(
  item: { title: string; chatTypeId: string | null; lastChatTypeId: string | null },
  expectedTitle: string,
  chatTypeId: string,
) {
  return item.title.trim() === expectedTitle && (item.chatTypeId === chatTypeId || item.lastChatTypeId === chatTypeId);
}
