{"version":3,"file":"index.mjs","names":[],"sources":["../../src/nes/nes.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * NES service module for JS apps — wraps `brookesia_service_nes` RPC.\n *\n * @example\n * import nes from '@brookesia/service/nes';\n * if (nes.available()) {\n *   nes.load({ rom_path: '/sdcard/roms/demo.nes' });\n *   nes.start();\n *   nes.setGamepadState({ a: true });\n *   brookesia.print(nes.getState());\n *   nes.close();\n * }\n */\n\nimport {\n  closeNes,\n  getNesState,\n  isNesAvailable,\n  loadNes,\n  openNes,\n  pauseNes,\n  resetNes,\n  resumeNes,\n  saveNes,\n  setNesGamepadState,\n  startNes,\n  stopNes,\n  type NesClientOptions,\n  type NesHandle,\n} from './nes-client.ts';\nimport type {\n  NesAudioMode,\n  NesConfig,\n  NesGamepadState,\n  NesState,\n  NesVideoArea,\n  NesVideoMode,\n} from './nes-types.ts';\n\nexport type { NesAudioMode, NesConfig, NesGamepadState, NesState, NesVideoArea, NesVideoMode };\n\nexport type NesModuleOptions = NesClientOptions;\n\ntype State = {\n  handle: NesHandle | null;\n  options: NesModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): NesHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isNesAvailable(state.options)) {\n    throw new Error('NES service is not available');\n  }\n  state.handle = openNes(state.options);\n  return state.handle;\n}\n\n/**\n * Inject a mock RPC host and reset the cached service handle (vitest / Node only).\n *\n * @param options - Client options, typically `{ host }` from `@brookesia/service/hosts/nes`\n */\nexport function configure(options: NesModuleOptions): void {\n  if (state.handle !== null) {\n    closeNes(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst nes = {\n  /** @returns `true` when NES service is registered */\n  available(): boolean {\n    return isNesAvailable(state.options);\n  },\n\n  /** Release the NES service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeNes(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /**\n   * Load a ROM image and optional save file.\n   *\n   * @param config - `{ rom_path, save_path, display_output_name, display_source_name? }`\n   */\n  load(config: NesConfig): void {\n    loadNes(handle(), config, state.options);\n  },\n\n  /** Start emulation (requires prior {@link load}). */\n  start(): void {\n    startNes(handle(), state.options);\n  },\n\n  /** Pause running emulation. */\n  pause(): void {\n    pauseNes(handle(), state.options);\n  },\n\n  /** Resume from paused state. */\n  resume(): void {\n    resumeNes(handle(), state.options);\n  },\n\n  /** Stop emulation. */\n  stop(): void {\n    stopNes(handle(), state.options);\n  },\n\n  /** Reset to loaded-ROM state without unloading. */\n  reset(): void {\n    resetNes(handle(), state.options);\n  },\n\n  /** Write save RAM to `save_path` from {@link load}. */\n  save(): void {\n    saveNes(handle(), state.options);\n  },\n\n  /**\n   * Push gamepad button state to the emulator.\n   *\n   * @param gamepad - `{ a, b, start, select, up, down, left, right, ... }`\n   */\n  setGamepadState(gamepad: NesGamepadState): void {\n    setNesGamepadState(handle(), gamepad, state.options);\n  },\n\n  /**\n   * @returns `'Idle'` | `'Loaded'` | `'Running'` | `'Paused'` | `'Stopped'`\n   */\n  getState(): NesState {\n    return getNesState(handle(), state.options);\n  },\n};\n\nexport default nes;\n"],"mappings":";;;;;;;;;;;;;;;AAmDA,MAAM,QAAe;CAAE,QAAQ;CAAM,SAAS,CAAC;AAAE;AAEjD,SAAS,SAAoB;CAC3B,IAAI,MAAM,WAAW,MACnB,OAAO,MAAM;CAEf,IAAI,CAAC,eAAe,MAAM,OAAO,GAC/B,MAAM,IAAI,MAAM,8BAA8B;CAEhD,MAAM,SAAS,QAAQ,MAAM,OAAO;CACpC,OAAO,MAAM;AACf;;;;;;AAOA,SAAgB,UAAU,SAAiC;CACzD,IAAI,MAAM,WAAW,MACnB,SAAS,MAAM,QAAQ,MAAM,OAAO;CAEtC,MAAM,SAAS;CACf,MAAM,UAAU;AAClB;AAEA,MAAM,MAAM;;CAEV,YAAqB;EACnB,OAAO,eAAe,MAAM,OAAO;CACrC;;CAGA,QAAc;EACZ,IAAI,MAAM,WAAW,MACnB;EAEF,SAAS,MAAM,QAAQ,MAAM,OAAO;EACpC,MAAM,SAAS;CACjB;;;;;;CAOA,KAAK,QAAyB;EAC5B,QAAQ,OAAO,GAAG,QAAQ,MAAM,OAAO;CACzC;;CAGA,QAAc;EACZ,SAAS,OAAO,GAAG,MAAM,OAAO;CAClC;;CAGA,QAAc;EACZ,SAAS,OAAO,GAAG,MAAM,OAAO;CAClC;;CAGA,SAAe;EACb,UAAU,OAAO,GAAG,MAAM,OAAO;CACnC;;CAGA,OAAa;EACX,QAAQ,OAAO,GAAG,MAAM,OAAO;CACjC;;CAGA,QAAc;EACZ,SAAS,OAAO,GAAG,MAAM,OAAO;CAClC;;CAGA,OAAa;EACX,QAAQ,OAAO,GAAG,MAAM,OAAO;CACjC;;;;;;CAOA,gBAAgB,SAAgC;EAC9C,mBAAmB,OAAO,GAAG,SAAS,MAAM,OAAO;CACrD;;;;CAKA,WAAqB;EACnB,OAAO,YAAY,OAAO,GAAG,MAAM,OAAO;CAC5C;AACF"}