{"version":3,"file":"ESPRMMatterNodeStorage.cjs","sources":["../../../src/storage/ESPRMMatterNodeStorage.ts"],"sourcesContent":["/*\n * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { ESPRMStorageAdapterInterface } from \"@espressif/rainmaker-base-sdk\";\nimport { ESPRMMatterStoredNodeRecord } from \"../types/node/stored\";\n\nconst MATTER_NODES_STORAGE_KEY = \"espressif.matter.stored_nodes\";\n\n/**\n * Persists commissioned Matter node records via the injected storage adaptor.\n * This class can be used to store and retrieve commissioned Matter node records as the cached data for the node.\n * \n * @example\n * const storage = new ESPRMMatterNodeStorage(new LocalStorageAdapter());\n * await storage.saveNode({ matterNodeId: \"123\", rainmakerNodeId: \"456\" });\n * const nodes = await storage.loadNodes();\n * console.log(nodes);\n * // [ESPRMMatterStoredNodeRecord]\n * await storage.removeNode(\"123\");\n * const nodes = await storage.loadNodes();\n * console.log(nodes);\n * // []\n */\nclass ESPRMMatterNodeStorage {\n  private readonly storage: ESPRMStorageAdapterInterface;\n\n  /**\n   * @param storage - Platform storage adaptor from SDK config\n   */\n  constructor(storage: ESPRMStorageAdapterInterface) {\n    this.storage = storage;\n  }\n\n  /**\n   * Saves or updates a commissioned Matter node record.\n   * @param record - Node snapshot to persist\n   */\n  async saveNode(record: ESPRMMatterStoredNodeRecord): Promise<void> {\n    const nodes = await this.loadNodes();\n    const index = nodes.findIndex(\n      (n) => n.matterNodeId === record.matterNodeId\n    );\n    if (index >= 0) {\n      nodes[index] = record;\n    } else {\n      nodes.push(record);\n    }\n    await this.storage.setItem(\n      MATTER_NODES_STORAGE_KEY,\n      JSON.stringify(nodes)\n    );\n  }\n\n  /**\n   * Loads all persisted Matter node records.\n   * @returns Array of stored node records\n   */\n  async loadNodes(): Promise<ESPRMMatterStoredNodeRecord[]> {\n    const raw = await this.storage.getItem(MATTER_NODES_STORAGE_KEY);\n    if (!raw) {\n      return [];\n    }\n    try {\n      const parsed = JSON.parse(raw) as ESPRMMatterStoredNodeRecord[];\n      return Array.isArray(parsed) ? parsed : [];\n    } catch {\n      return [];\n    }\n  }\n\n  /**\n   * Removes a node record by Matter node ID.\n   * @param matterNodeId - Matter operational node ID\n   */\n  async removeNode(matterNodeId: string): Promise<void> {\n    const nodes = await this.loadNodes();\n    const filtered = nodes.filter((n) => n.matterNodeId !== matterNodeId);\n    await this.storage.setItem(\n      MATTER_NODES_STORAGE_KEY,\n      JSON.stringify(filtered)\n    );\n  }\n}\n\nexport { ESPRMMatterNodeStorage, MATTER_NODES_STORAGE_KEY };\n"],"names":[],"mappings":";;AAAA;;;;AAIG;AAKH,MAAM,wBAAwB,GAAG;AAEjC;;;;;;;;;;;;;;AAcG;AACH,MAAM,sBAAsB,CAAA;AACT,IAAA,OAAO;AAExB;;AAEG;AACH,IAAA,WAAA,CAAY,OAAqC,EAAA;AAC/C,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;IACxB;AAEA;;;AAGG;IACH,MAAM,QAAQ,CAAC,MAAmC,EAAA;AAChD,QAAA,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,SAAS,EAAE;AACpC,QAAA,MAAM,KAAK,GAAG,KAAK,CAAC,SAAS,CAC3B,CAAC,CAAC,KAAK,CAAC,CAAC,YAAY,KAAK,MAAM,CAAC,YAAY,CAC9C;AACD,QAAA,IAAI,KAAK,IAAI,CAAC,EAAE;AACd,YAAA,KAAK,CAAC,KAAK,CAAC,GAAG,MAAM;QACvB;aAAO;AACL,YAAA,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC;QACpB;AACA,QAAA,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CACxB,wBAAwB,EACxB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CACtB;IACH;AAEA;;;AAGG;AACH,IAAA,MAAM,SAAS,GAAA;QACb,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,wBAAwB,CAAC;QACh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