import { generateRandomTagColor } from './utils/colors';

type ColorGenerator = () => string;

interface TagManagerOptions {
  colorGenerator?: ColorGenerator;
}

interface TagPayload {
  label: string;
  color: string;
}

class TagManager {
  private readonly colorGenerator: ColorGenerator;

  constructor({ colorGenerator = generateRandomTagColor }: TagManagerOptions = {}) {
    this.colorGenerator = colorGenerator;
  }

  normalizeLabel(label?: string | null): string {
    return label?.trim?.() || '';
  }

  buildPayload({ label, color }: { label?: string | null; color?: string | null } = {}): TagPayload {
    const normalizedLabel = this.normalizeLabel(label);
    if (!normalizedLabel) {
      throw new Error('Tag label is required.');
    }
    const trimmedColor = color?.trim?.() || null;
    return {
      label: normalizedLabel,
      color: trimmedColor || this.colorGenerator(),
    };
  }
}

export default TagManager;
