import { generateNodeId } from './strategyTypes';

export const START_NODE_ID = '__strategy_start__';
export const DEFAULT_START_CANDLE = '1m';

export const STRATEGY_CANDLE_TYPES = [
  '1m', '3m', '5m', '15m', '30m', '1h', '4h', '1d',
] as const;

export type StrategyCandleType = (typeof STRATEGY_CANDLE_TYPES)[number];

export type StartNodeMeta = {
  candleType: string;
};

/** START가 2개 이상일 때 분기 간 결합 연산 */
export type StartCombineOp = 'AND' | 'OR';

export const DEFAULT_START_COMBINE_OP: StartCombineOp = 'AND';

export function isStartNodeId(id: string): boolean {
  return id === START_NODE_ID || id.startsWith('__strategy_start_');
}

export function createStartNodeId(): string {
  return `__strategy_start_${generateNodeId()}__`;
}

export function defaultStartMeta(): Record<string, StartNodeMeta> {
  return { [START_NODE_ID]: { candleType: DEFAULT_START_CANDLE } };
}

export function normalizeStartCandleType(value: string | undefined | null): string {
  const raw = (value ?? DEFAULT_START_CANDLE).trim().toLowerCase();
  if (raw === '1d') return '1d';
  return (STRATEGY_CANDLE_TYPES as readonly string[]).includes(raw) ? raw : DEFAULT_START_CANDLE;
}

export function formatStartCandleLabel(candleType: string): string {
  return normalizeStartCandleType(candleType);
}
