import type { LabelColorOption, LabelEditorForm } from '../../types';

/**
 * 文件职责：标签页面常量与基础构造方法。
 */
export const LABEL_COLOR_OPTIONS: LabelColorOption[] = [
  { value: '#1890ff' },
  { value: '#ff4d4f' },
  { value: '#fa8c16' },
  { value: '#52c41a' },
  { value: '#722ed1' },
  { value: '#eb2f96' },
  { value: '#13c2c2' },
  { value: '#999999' },
];

export const DEFAULT_LABEL_COLOR = LABEL_COLOR_OPTIONS[0]?.value ?? '#1890ff';

export function normalizeLabelColor(value: null | string | undefined) {
  const normalized = String(value ?? '')
    .trim()
    .toLowerCase();
  if (!normalized) return DEFAULT_LABEL_COLOR;
  if (normalized === '#999') return '#999999';
  if (/^#[\da-f]{6}$/.test(normalized)) return normalized;
  if (/^#[\da-f]{3}$/.test(normalized)) {
    const r = normalized[1];
    const g = normalized[2];
    const b = normalized[3];
    return `#${r}${r}${g}${g}${b}${b}`;
  }
  return DEFAULT_LABEL_COLOR;
}

export function createDefaultLabelForm(sort = 0): LabelEditorForm {
  return {
    name: '',
    color: DEFAULT_LABEL_COLOR,
    sort,
    status: 'enabled',
  };
}
