/** Internal action bag used to build the public document namespace. */
export interface AsukaDocumentActions {
  addObject?: (...args: unknown[]) => unknown;
  updateObject?: (...args: unknown[]) => unknown;
  removeObject?: (...args: unknown[]) => unknown;
  readDocumentSnapshot?: (...args: unknown[]) => unknown;
  loadDocumentSnapshot?: (...args: unknown[]) => unknown;
  rebuildViewFromDocumentState?: (...args: unknown[]) => unknown;
  syncDocumentObjectsForView?: (...args: unknown[]) => unknown;
}

/** Factory configuration for the internal `createDocumentNamespace` factory. */
export interface DocumentNamespaceConfig {
  actions?: AsukaDocumentActions;
}

/** Public document namespace returned by the internal document factory. */
export interface AsukaDocumentNamespace {
  addObject: (...args: unknown[]) => unknown;
  updateObject: (...args: unknown[]) => unknown;
  removeObject: (...args: unknown[]) => unknown;
  readSnapshot: (...args: unknown[]) => unknown;
  loadSnapshot: (...args: unknown[]) => unknown;
  rebuildView: (...args: unknown[]) => unknown;
  rebuildActiveView: (...args: unknown[]) => unknown;
  syncViewObjects: (...args: unknown[]) => unknown;
}

/** Internal runtime bag used to build the public renderer/runtime namespace. */
export interface AsukaRuntimeHooks {
  refreshAfterViewLoad?: (...args: unknown[]) => unknown;
  syncDrawingZoneVisuals?: (...args: unknown[]) => unknown;
  clipPaths?: unknown;
  snapLines?: unknown;
  curvedGuides?: unknown;
  selectionOutline?: unknown;
  runtimeObjectIndex?: unknown;
  fixedSlots?: unknown;
  textEditing?: unknown;
  objectModified?: unknown;
  objectCollection?: unknown;
  drawingAreas?: unknown;
  drawingAreasInteraction?: unknown;
  lifecycle?: unknown;
}

/** Factory configuration for the internal `createRuntimeNamespace` factory. */
export interface RuntimeNamespaceConfig {
  runtime?: AsukaRuntimeHooks;
}

/** Public runtime namespace returned by the internal runtime factory. */
export interface AsukaRuntimeNamespace {
  refresh: (...args: unknown[]) => unknown;
  syncDrawingAreas: (...args: unknown[]) => unknown;
  clipPaths?: unknown;
  snapLines?: unknown;
  curvedGuides?: unknown;
  selectionOutline?: unknown;
  runtimeObjectIndex?: unknown;
  fixedSlots?: unknown;
  textEditing?: unknown;
  objectModified?: unknown;
  objectCollection?: unknown;
  drawingAreas?: unknown;
  drawingAreasInteraction?: unknown;
  lifecycle?: unknown;
}

/**
 * Build the document-first namespace attached to low-level designer instances.
 *
 * This helper exposes a small action bag under stable method names.
 */
export const createDocumentNamespace = (config: DocumentNamespaceConfig): AsukaDocumentNamespace => {
  const actions = config?.actions ?? {};
  const rebuildView = (...args: unknown[]) => actions?.rebuildViewFromDocumentState?.(...args);
  const readSnapshot = (...args: unknown[]) => actions?.readDocumentSnapshot?.(...args);
  const loadSnapshot = (...args: unknown[]) => actions?.loadDocumentSnapshot?.(...args);
  const syncViewObjects = (...args: unknown[]) => actions?.syncDocumentObjectsForView?.(...args);

  return {
    addObject: (...args: unknown[]) => actions?.addObject?.(...args),
    updateObject: (...args: unknown[]) => actions?.updateObject?.(...args),
    removeObject: (...args: unknown[]) => actions?.removeObject?.(...args),
    readSnapshot,
    loadSnapshot,
    rebuildView,
    rebuildActiveView: rebuildView,
    syncViewObjects,
  };
};

/**
 * Build the renderer/runtime namespace attached to low-level designer instances.
 *
 * It exposes helper groups such as snapping visuals, drawing-area internals and
 * text-editing internals.
 */
export const createRuntimeNamespace = (config: RuntimeNamespaceConfig): AsukaRuntimeNamespace => {
  const runtime = config?.runtime ?? {};
  const refresh = (...args: unknown[]) => runtime?.refreshAfterViewLoad?.(...args);
  const syncDrawingAreas = (...args: unknown[]) => runtime?.syncDrawingZoneVisuals?.(...args);

  return {
    refresh,
    syncDrawingAreas,
    clipPaths: runtime?.clipPaths,
    snapLines: runtime?.snapLines,
    curvedGuides: runtime?.curvedGuides,
    selectionOutline: runtime?.selectionOutline,
    runtimeObjectIndex: runtime?.runtimeObjectIndex,
    fixedSlots: runtime?.fixedSlots,
    textEditing: runtime?.textEditing,
    objectModified: runtime?.objectModified,
    objectCollection: runtime?.objectCollection,
    drawingAreas: runtime?.drawingAreas,
    drawingAreasInteraction: runtime?.drawingAreasInteraction,
    lifecycle: runtime?.lifecycle,
  };
};
