"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatterServer = void 0;
const matter_node_js_1 = require("@project-chip/matter-node.js");
const log_1 = require("@project-chip/matter-node.js/log");
const storage_1 = require("@project-chip/matter-node.js/storage");
const node_fs_1 = __importDefault(require("node:fs"));
const LOG_LEVEL = Number(process.env.NODE_RED_MATTER_LOG_LEVEL);
switch (LOG_LEVEL) {
    case 0:
        log_1.Logger.defaultLogLevel = log_1.Level.DEBUG;
        break;
    case 1:
        log_1.Logger.defaultLogLevel = log_1.Level.INFO;
        break;
    case 2:
        log_1.Logger.defaultLogLevel = log_1.Level.WARN;
        break;
    case 3:
        log_1.Logger.defaultLogLevel = log_1.Level.ERROR;
        break;
    case 4:
        log_1.Logger.defaultLogLevel = log_1.Level.FATAL;
        break;
    default:
        log_1.Logger.defaultLogLevel = log_1.Level.ERROR;
}
class MatterServer {
    matterServer;
    storageLocation;
    async init({ storageLocation }) {
        this.storageLocation = storageLocation;
        const storage = new storage_1.StorageBackendDisk(storageLocation);
        const storageManager = new storage_1.StorageManager(storage);
        await storageManager.initialize();
        this.matterServer = new matter_node_js_1.MatterServer(storageManager);
    }
    addCommissioningServer(commissioningServer) {
        if (this.matterServer == null) {
            throw new Error('Matter server not initialized');
        }
        this.matterServer?.addCommissioningServer(commissioningServer);
    }
    commissioningControllers = [];
    connectAllCommissioningControllers() {
        return Promise.all(this.commissioningControllers.map((commissioningController) => {
            return commissioningController.connect();
        }));
    }
    addCommissioningController(commissioningController) {
        if (this.matterServer == null) {
            throw new Error('Matter server not initialized');
        }
        this.commissioningControllers.push(commissioningController);
        this.matterServer?.addCommissioningController(commissioningController);
    }
    async start() {
        if (this.matterServer == null) {
            throw new Error('Matter server not initialized');
        }
        await this.matterServer?.start();
    }
    async stop() {
        if (this.matterServer == null) {
            throw new Error('Matter server not initialized');
        }
        await this.matterServer?.close();
    }
    async destroy() {
        if (this.matterServer == null) {
            throw new Error('Matter server not initialized');
        }
        await this.matterServer?.close();
        if (this.storageLocation == null) {
            throw new Error('Storage location not initialized');
        }
        node_fs_1.default.rmdirSync(this.storageLocation, { recursive: true });
    }
}
exports.MatterServer = MatterServer;
