// src/serialization/index.ts
// Serialization Module - 5-Representation Parity
//
// This module provides serialization to/from 5 equivalent representations:
// - HTML: Visual representation using custom elements
// - Fluent: Builder DSL for TypeScript/JavaScript
// - S-Expr: LISP syntax
// - Topics: Matrix pub/sub structure
// - JSON: Schema definition
//
// References:
// - Vision/14-HOMOICONIC-ARCHITECTURE.md - Pillars 15-16 (SERIALIZE/EVAL)
// - Vision/29-CONTENT-ADDRESSED-EXPRESSION-NETWORK.md - Interface hashing
// - Vision/31-LISP-TESTING-VISION.md - Mock generation from interfaces
// - Vision/20-COMPOSITION-DSL.md - Three syntaxes, one tree
// - Vision/24-STANDARD-INTERFACES.md - IDL interoperability

// Types (individual interface files - Rich Hickey: no junk drawer)
export type { SerializationFormat } from './SerializationFormat';
export type { IParamSchema } from './IParamSchema';
export type { IMethodSchema } from './IMethodSchema';
export type { IEventSchema } from './IEventSchema';
export type { IComponentSchema } from './IComponentSchema';
export type { ISerializationOptions } from './ISerializationOptions';
export type { IParseResult } from './IParseResult';

// Interface (from ISerializable.ts)
export type { ISerializable } from './ISerializable';

// Base class (from Serializable.ts - separated to avoid circular deps)
export { Serializable } from './Serializable';

// Converters
export { toHtml } from './toHtml';
export { fromHtml } from './fromHtml';
export { toFluent } from './toFluent';
export { fromFluent } from './fromFluent';
export { toSExpr, valueToSExpr } from './toSExpr';
export { fromSExpr, fromSExprValue, sexprToValue } from './fromSExpr';
export { toTopics, getTopicsArray, getSubscriptionMap, getPublicationMap } from './toTopics';
export { fromTopics } from './fromTopics';
export { toJson, toJsonSchema } from './toJson';

// Import for local use in convenience functions
import { toHtml as _toHtml } from './toHtml';
import { fromHtml as _fromHtml } from './fromHtml';
import { toFluent as _toFluent } from './toFluent';
import { fromFluent as _fromFluent } from './fromFluent';
import { toSExpr as _toSExpr } from './toSExpr';
import { fromSExpr as _fromSExpr } from './fromSExpr';
import { toTopics as _toTopics } from './toTopics';
import { fromTopics as _fromTopics } from './fromTopics';
import { toJson as _toJson } from './toJson';

import type { IComponentSchema } from './IComponentSchema';
import type { SerializationFormat } from './SerializationFormat';
import type { ISerializationOptions } from './ISerializationOptions';
import type { IParseResult } from './IParseResult';

/**
 * Serialize component schema to any format
 *
 * @param schema - The component schema to serialize
 * @param format - Target format: 'html', 'fluent', 'sexpr', 'topics', or 'json'
 * @param options - Optional serialization options
 * @returns Serialized string representation
 *
 * @example
 * serialize({ name: 'Button', accepts: {}, emits: {} }, 'html')
 * // → <matrix-button></matrix-button>
 */
export function serialize(
  schema: IComponentSchema,
  format: SerializationFormat,
  options?: ISerializationOptions
): string {
  switch (format) {
    case 'html':
      return _toHtml(schema, options);
    case 'fluent':
      return _toFluent(schema, options);
    case 'sexpr':
      return _toSExpr(schema, options);
    case 'topics':
      return _toTopics(schema, options);
    case 'json':
      return _toJson(schema, options);
    default:
      throw new Error(`Unknown format: ${format}`);
  }
}

/**
 * Parse serialized component from any format
 *
 * @param source - Source string to parse
 * @param format - Source format: 'html', 'fluent', 'sexpr', 'topics', or 'json'
 * @returns Parse result with success/error status
 *
 * @example
 * parse('<matrix-button></matrix-button>', 'html')
 * // → { success: true, value: { name: 'Button', accepts: {}, emits: {} } }
 */
export function parse(
  source: string,
  format: SerializationFormat
): IParseResult<IComponentSchema> {
  switch (format) {
    case 'html':
      return _fromHtml(source);
    case 'sexpr':
      return _fromSExpr(source);
    case 'json':
      try {
        const value = JSON.parse(source);
        return { success: true, value };
      } catch (err) {
        return {
          success: false,
          error: `JSON parse error: ${err instanceof Error ? err.message : String(err)}`,
        };
      }
    case 'fluent':
      return _fromFluent(source);
    case 'topics':
      return _fromTopics(source);
    default:
      return { success: false, error: `Unknown format: ${format}` };
  }
}
