/**
 * Export utilities for color palettes
 */

export interface ExportColor {
  name?: string;
  hex: string;
}

/**
 * Export colors as CSS variables
 */
export function exportAsCSS(colors: ExportColor[]): string {
  const variables = colors
    .map((color, index) => {
      const name = color.name || `color-${index + 1}`;
      return `  --${name}: ${color.hex};`;
    })
    .join('\n');

  return `:root {\n${variables}\n}`;
}

/**
 * Export colors as SCSS variables
 */
export function exportAsSCSS(colors: ExportColor[]): string {
  return colors
    .map((color, index) => {
      const name = color.name || `color-${index + 1}`;
      return `$${name}: ${color.hex};`;
    })
    .join('\n');
}

/**
 * Export colors as Tailwind config
 */
export function exportAsTailwind(colors: ExportColor[]): string {
  const colorEntries = colors
    .map((color, index) => {
      const name = color.name || `color-${index + 1}`;
      return `        '${name}': '${color.hex}',`;
    })
    .join('\n');

  return `module.exports = {\n  theme: {\n    extend: {\n      colors: {\n${colorEntries}\n      },\n    },\n  },\n};`;
}

/**
 * Export colors as JSON
 */
export function exportAsJSON(colors: ExportColor[]): string {
  const colorObjects = colors.map((color, index) => ({
    name: color.name || `color-${index + 1}`,
    hex: color.hex,
  }));

  return JSON.stringify({ colors: colorObjects }, null, 2);
}

/**
 * Export colors as JavaScript array
 */
export function exportAsJavaScript(colors: ExportColor[]): string {
  const colorArray = colors.map((c) => `'${c.hex}'`).join(', ');
  return `const colors = [${colorArray}];`;
}

/**
 * Download text as file
 */
export function downloadAsFile(content: string, filename: string, mimeType: string = 'text/plain') {
  const blob = new Blob([content], { type: mimeType });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = filename;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
}
