/**
 * Inheritance and import resolution for PromptScript files.
 *
 * Handles file loading, dependency resolution, and merging of inherited properties
 * to produce a fully resolved AST.
 *
 * @packageDocumentation
 */

// Main resolver
export { Resolver, createResolver } from './resolver.js';
export type { ResolverOptions, ResolvedAST } from './resolver.js';

// File loader
export {
  FileLoader,
  REGISTRY_MARKER_PREFIX,
  parseRegistryMarker,
  buildRegistryMarker,
} from './loader.js';
export type { LoaderOptions } from './loader.js';

// Registry implementations
export {
  FileSystemRegistry,
  HttpRegistry,
  CompositeRegistry,
  createFileSystemRegistry,
  createHttpRegistry,
  createCompositeRegistry,
  type Registry,
  type FileSystemRegistryOptions,
  type HttpRegistryOptions,
  type CompositeRegistryOptions,
} from './registry.js';

// Git registry
export {
  GitRegistry,
  createGitRegistry,
  validateRemoteAccess,
  GitCloneError,
  GitAuthError,
  GitRefNotFoundError,
  type GitRegistryOptions,
  type GitAuthOptions,
  type RemoteValidation,
} from './git-registry.js';

// Git utilities
export {
  isGitUrl,
  parseGitUrl,
  normalizeGitUrl,
  buildAuthenticatedUrl,
  getCacheKey,
  parseVersionedPath,
  isKnownGitHost,
  getWebUrl,
  type ParsedGitUrl,
  type ParsedVersionedPath,
} from './git-url-utils.js';

// Git cache manager
export {
  GitCacheManager,
  createGitCacheManager,
  type GitCacheManagerOptions,
  type CacheMetadata,
  type CacheEntry,
} from './git-cache-manager.js';

// Registry cache (hierarchical, for semver tag resolution)
export { RegistryCache } from './registry-cache.js';

// Vendor registry (offline / CI builds)
export { VendorRegistry, createVendorRegistry } from './vendor-registry.js';

// Inheritance resolution
export { resolveInheritance } from './inheritance.js';

// Import resolution
export {
  resolveUses,
  isImportMarker,
  getImportAlias,
  getOriginalBlockName,
  IMPORT_MARKER_PREFIX,
} from './imports.js';

// Block alias normalization
export { normalizeBlockAliases } from './normalize.js';

// Extension resolution
export { applyExtends } from './extensions.js';

// Skill composition resolution
export { resolveSkillComposition } from './skill-composition.js';
export type { CompositionOptions } from './skill-composition.js';

// Guard requires resolution
export { resolveGuardRequires } from './guard-requires.js';
export type { GuardRequiresOptions } from './guard-requires.js';

// Skill resolution
export {
  resolveNativeSkills,
  resolveNativeCommands,
  parseSkillMd,
  skillNameFromPath,
  interpolateSkillContent,
} from './skills.js';
export type { NativeSkillOptions, ParsedSkillMd } from './skills.js';

// Skill frontmatter validation (Agent Skills spec compliance)
export { validateSkillFrontmatter, formatSkillValidationIssues } from './skill-validation.js';
export type {
  SkillValidationIssue,
  SkillValidationOptions,
  SkillValidationResult,
  SkillValidationSeverity,
} from './skill-validation.js';

// Template interpolation (re-export from core for backwards compatibility)
export {
  bindParams,
  interpolateText,
  interpolateContent,
  interpolateAST,
  isTemplateExpression,
  type TemplateContext,
} from '@promptscript/core';

// Alias resolver
export {
  expandAlias,
  findFallbackUrl,
  validateAlias,
  validateRegistriesConfig,
  type ExpandedAlias,
  type RegistriesValidationResult,
} from './alias-resolver.js';

// Auto-discovery
export { discoverNativeContent } from './auto-discovery.js';

// AST factory helpers
export { makeBlock, makeObjectContent, makeTextContent, VIRTUAL_LOC } from './ast-factory.js';

// Reference integrity hashing
export { hashContent, buildReferenceKey, isInsideCachePath } from './reference-hasher.js';

// Standalone resolve function
import type { ResolvedAST, ResolverOptions } from './resolver.js';
import { createResolver } from './resolver.js';

/**
 * Options for standalone resolve function.
 */
export interface ResolveOptions extends ResolverOptions {
  /** Reuse an existing resolver instance */
  resolver?: Resolver;
}

// Forward declaration to avoid circular deps
interface Resolver {
  resolve(entryPath: string): Promise<ResolvedAST>;
}

/**
 * Resolve a PromptScript file with a standalone function.
 *
 * @param entryPath - Path to the entry file
 * @param options - Resolution options
 * @returns Resolved AST with sources and errors
 *
 * @example
 * ```typescript
 * import { resolve } from '@promptscript/resolver';
 *
 * const result = await resolve('./project.prs', {
 *   registryPath: '/path/to/registry',
 *   localPath: process.cwd(),
 * });
 *
 * if (result.ast) {
 *   console.log('Resolved successfully');
 * }
 * ```
 */
export async function resolve(entryPath: string, options: ResolveOptions): Promise<ResolvedAST> {
  const { resolver, ...resolverOptions } = options;

  if (resolver) {
    return resolver.resolve(entryPath);
  }

  const r = createResolver(resolverOptions);
  return r.resolve(entryPath);
}
