{"version":3,"file":"index.mjs","names":[],"sources":["../../src/sntp/sntp.ts"],"sourcesContent":["/* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD */\n/* SPDX-License-Identifier: Apache-2.0 */\n\n/**\n * SNTP service module for JS apps — wraps `brookesia_service_sntp` RPC.\n *\n * @example\n * import sntp from '@brookesia/service/sntp';\n * if (sntp.available()) {\n *   sntp.setServers(['pool.ntp.org']);\n *   sntp.setTimezone('CST-8');\n *   sntp.start();\n *   brookesia.print(sntp.getState());\n *   sntp.close();\n * }\n */\n\nimport {\n  closeSntp,\n  getServers,\n  getSntpState,\n  getTimezone,\n  isSntpAvailable,\n  isTimeSynced,\n  loadSntpData,\n  openSntp,\n  resetSntpData,\n  setServers,\n  setTimezone,\n  startSntp,\n  stopSntp,\n  type SntpClientOptions,\n  type SntpHandle,\n} from './sntp-client.ts';\nimport type { SntpState } from './sntp-types.ts';\n\nexport type { SntpState };\n\nexport type SntpModuleOptions = SntpClientOptions;\n\ntype State = {\n  handle: SntpHandle | null;\n  options: SntpModuleOptions;\n};\n\nconst state: State = { handle: null, options: {} };\n\nfunction handle(): SntpHandle {\n  if (state.handle !== null) {\n    return state.handle;\n  }\n  if (!isSntpAvailable(state.options)) {\n    throw new Error('SNTP service is not available');\n  }\n  state.handle = openSntp(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/sntp`\n */\nexport function configure(options: SntpModuleOptions): void {\n  if (state.handle !== null) {\n    closeSntp(state.handle, state.options);\n  }\n  state.handle = null;\n  state.options = options;\n}\n\nconst sntp = {\n  /** @returns `true` when the SNTP service is registered */\n  available(): boolean {\n    return isSntpAvailable(state.options);\n  },\n\n  /** Release the SNTP service binding. */\n  close(): void {\n    if (state.handle === null) {\n      return;\n    }\n    closeSntp(state.handle, state.options);\n    state.handle = null;\n  },\n\n  /**\n   * Set NTP server hostnames.\n   *\n   * @param servers - e.g. `['pool.ntp.org', 'cn.pool.ntp.org']`\n   */\n  setServers(servers: string[]): void {\n    setServers(handle(), servers, state.options);\n  },\n\n  /**\n   * Set local timezone string.\n   *\n   * @param timezone - POSIX TZ string, e.g. `'CST-8'`\n   */\n  setTimezone(timezone: string): void {\n    setTimezone(handle(), timezone, state.options);\n  },\n\n  /** Start NTP synchronization. */\n  start(): void {\n    startSntp(handle(), state.options);\n  },\n\n  /** Stop NTP synchronization. */\n  stop(): void {\n    stopSntp(handle(), state.options);\n  },\n\n  /** @returns Configured NTP server list */\n  getServers(): string[] {\n    return getServers(handle(), state.options);\n  },\n\n  /** @returns Configured timezone string */\n  getTimezone(): string {\n    return getTimezone(handle(), state.options);\n  },\n\n  /**\n   * @returns Sync state machine value\n   * e.g. `'Stopped'`, `'Running'`, `'Syncing'`, `'Synced'`\n   */\n  getState(): SntpState {\n    return getSntpState(handle(), state.options);\n  },\n\n  /** @returns `true` when system time has been synchronized */\n  isTimeSynced(): boolean {\n    return isTimeSynced(handle(), state.options);\n  },\n\n  /** Load persisted SNTP settings. */\n  loadData(): void {\n    loadSntpData(handle(), state.options);\n  },\n\n  /** Reset SNTP settings to defaults. */\n  resetData(): void {\n    resetSntpData(handle(), state.options);\n  },\n};\n\nexport default sntp;\n"],"mappings":";;;;;;;;;;;;;;;AA6CA,MAAM,QAAe;CAAE,QAAQ;CAAM,SAAS,CAAC;AAAE;AAEjD,SAAS,SAAqB;CAC5B,IAAI,MAAM,WAAW,MACnB,OAAO,MAAM;CAEf,IAAI,CAAC,gBAAgB,MAAM,OAAO,GAChC,MAAM,IAAI,MAAM,+BAA+B;CAEjD,MAAM,SAAS,SAAS,MAAM,OAAO;CACrC,OAAO,MAAM;AACf;;;;;;AAOA,SAAgB,UAAU,SAAkC;CAC1D,IAAI,MAAM,WAAW,MACnB,UAAU,MAAM,QAAQ,MAAM,OAAO;CAEvC,MAAM,SAAS;CACf,MAAM,UAAU;AAClB;AAEA,MAAM,OAAO;;CAEX,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