export type FeatureCategory =
  | 'organize'
  | 'convert-in'
  | 'convert-out'
  | 'optimize'
  | 'security'
  | 'forms'
  | 'ocr'
  | 'sign';

/** Context passed to a feature when launched from inside the editor. */
export interface ToolContext {
  /** Current editing document bytes, if launched from the editor. */
  currentBytes?: () => Uint8Array | null;
  currentName?: () => string;
  /** Replace the editor's document with new bytes (e.g. after decrypt/compress). */
  loadIntoEditor?: (bytes: Uint8Array, name: string) => void;
}

export interface FeatureDef {
  id: string;
  title: string;
  desc: string;
  icon: string;
  category: FeatureCategory;
  /** Opens the feature's own modal UI. */
  run(ctx: ToolContext): void;
}

const registry: FeatureDef[] = [];

export function registerFeature(def: FeatureDef) {
  if (!registry.some((f) => f.id === def.id)) registry.push(def);
}

export function allFeatures(): FeatureDef[] {
  return registry;
}

export function featuresByCategory(cat: FeatureCategory): FeatureDef[] {
  return registry.filter((f) => f.category === cat);
}

export function getFeature(id: string): FeatureDef | undefined {
  return registry.find((f) => f.id === id);
}

export const CATEGORY_LABELS: Record<FeatureCategory, string> = {
  organize: 'Organize pages',
  'convert-in': 'Create a PDF',
  'convert-out': 'Convert from PDF',
  optimize: 'Optimize',
  security: 'Security',
  forms: 'Forms',
  ocr: 'OCR',
  sign: 'Sign',
};
