"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const INetworkInfo_1 = require("./INetworkInfo");
const Validate_1 = __importDefault(require("../../Validate/Validate"));
const INetwork_1 = require("./INetwork");
/**
 * The `sos.management.network` API groups together networking methods. For Wi-Fi setup, use the [Wi-Fi API](https://developers.signageos.io/sdk/sos_management/wifi).
 *
 * <details>
 *     <summary>Network Management Capabilities</summary>
 * 		| Capability | Description |
 * 		|:------------|:-------------|
 * 		| `NETWORK_INFO` | If device supports returning network information |
 *
 * 		If you want to check if the device supports this capability, use [`sos.management.supports()`](https://developers.signageos.io/sdk/sos_management/#supports).
 * </details>
 */
class Network {
    messagePrefix;
    postMessage;
    /** @internal */
    constructor(messagePrefix, postMessage) {
        this.messagePrefix = messagePrefix;
        this.postMessage = postMessage;
    }
    /** @deprecated Use `sos.management.network.listInterfaces()` instead. */
    async getActiveInfo() {
        const { networkInfo } = await this.postMessage({
            type: this.getMessage('get_network_info'),
        });
        return networkInfo;
    }
    /**
     * The `listInterface()` method returns a list of all network interfaces, and their information like MAC address, IP address, etc.
     * Wi-Fi connection strength is described as a percentage in the range from 0 to 100, linearly converted from dBm -90 to -30, respectively, based on the platform.
     *
     * @returns {Promise<INetworkInterface[]>} Resolves to an array of network interfaces with their information.
     * @since 4.9.0
     *
     * @example // {@link https://github.com/signageos/applet-examples/tree/master/examples/management-js-api/network | Applet Example with Network Interfaces}
     *
     * @example
     * const interfaces = await sos.management.network.listInterfaces();
     * interfaces.forEach((iface) => {
     *    console.log(`Interface: ${iface.name}, IP: ${iface.ipAddress}, MAC: ${iface.macAddress}`);
     * });
     */
    async listInterfaces() {
        const { interfaces } = await this.postMessage({
            type: this.getMessage('get_network_interfaces'),
        });
        return interfaces;
    }
    /** @internal */
    async setManual(...args) {
        if (typeof args[0] === 'string') {
            const [interfaceName, options] = args;
            (0, Validate_1.default)({ interfaceName }).required().string();
            (0, Validate_1.default)({ options }).required().object(INetworkInfo_1.VINetworkOptions);
            await this.postMessage({
                type: this.getMessage('set_network_manual_v2'),
                interfaceName,
                options,
            });
        }
        else {
            const [options] = args;
            (0, Validate_1.default)({ options }).required().object(INetworkInfo_1.VINetworkOptionsLegacy);
            await this.postMessage({
                type: this.getMessage('set_network_manual'),
                options,
            });
        }
    }
    /** @internal */
    async setDHCP(...args) {
        if (args[0] === 'ethernet' || args[0] === 'wifi') {
            const [networkInterface] = args;
            (0, Validate_1.default)({ networkInterface }).required().string(INetworkInfo_1.VNetworkInterface);
            await this.postMessage({
                type: this.getMessage('set_network_dhcp'),
                networkInterface,
            });
        }
        else {
            const [interfaceName] = args;
            (0, Validate_1.default)({ interfaceName }).required().string();
            await this.postMessage({
                type: this.getMessage('set_network_dhcp_v2'),
                interfaceName,
            });
        }
    }
    /**
     * The `disableInterface()` turns off a selected network interface.
     *
     * :::warning
     * Don't use this method to disable Wi-Fi. Use [Wi-Fi API](https://developers.signageos.io/sdk/sos_management/wifi) to turn Wi-Fi on/off.
     * :::
     *
     * @param interfaceName The interface name which can be retrieved from the list of interfaces returned by `listInterfaces()`
     * @return {Promise<void>} A promise that resolves when the network interface is disabled.
     * @throws {Error} If the interface name is invalid.
     * @throws {Error} If the device does not support network management.
     * @since 4.13.0
     *
     * @example
     * await sos.management.network.disableInterface('eth0');
     */
    async disableInterface(interfaceName) {
        (0, Validate_1.default)({ interfaceName }).required().string();
        await this.postMessage({
            type: this.getMessage('disable_network_interface'),
            interfaceName,
        });
    }
    /**
     * The `importCertificate()` method imports a certificate to the device. The certificate can then be used when connecting to a Wi-Fi network using the [Wi-Fi API and EAP authentification](https://developers.signageos.io/sdk/sos_management/wifi).
     *
     * :::note
     * - This API supports only PEM formatted certificates.
     * - Only EAP certificates are supported for now.
     * :::
     *
     * @param details The certificate details.
     * @param details.type The type of the certificate is for.
     * @param details.caCertificate The CA certificate in PEM format. Required for 'PEAP' and 'EAP-TTLS'.
     * @param details.clientCertificate The client certificate in PEM format. Required for 'EAP-TLS'.
     * @param details.clientKey The private key for the client certificate in PEM format. Required for 'EAP-TLS'.
     * @param details.clientCertificatePassword The password for the private key, if it's encrypted. Optional.
     * @returns {Promise<void>} A promise that resolves when the certificate is imported.
     * @throws {Error} If the certificate details are invalid.
     * @throws {Error} If the device does not support certificate import.
     * @since 8.3.0
     *
     * @example
     * // Importing an EAP-TLS certificate
     * const certDetails = {
     *   type: 'EAP-TLS',
     *   caCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----',
     *   clientCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----',
     *   clientKey: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----',
     *   clientCertificatePassword: 'your_password', // if the key is encrypted
     * };
     * await sos.management.network.importCertificate(certDetails);
     *
     * // Importing a PEAP certificate
     * const certDetailsEapPeap = {
     *   type: 'PEAP',
     *   caCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----',
     * };
     * await sos.management.network.importCertificate(certDetailsEapPeap);
     *
     * // Importing an EAP-TTLS certificate
     * const certDetailsEapTtls = {
     *   type: 'EAP-TTLS',
     *   caCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----',
     * };
     * await sos.management.network.importCertificate(certDetailsEapTtls);
     */
    async importCertificate(details) {
        (0, Validate_1.default)({ details }).required().object(INetwork_1.VCertificateEapDetails);
        await this.postMessage({
            type: this.getMessage('import_certificate'),
            details,
        });
    }
    getMessage(name) {
        return this.messagePrefix + '.' + name;
    }
}
exports.default = Network;
//# sourceMappingURL=Network.js.map