"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const Validate_1 = __importDefault(require("../Validate/Validate"));
/**
 * The `sos.debug` API groups together methods for checking the state of remote debug mode.
 *
 * :::note
 * - This debug is not the "native debug" of the device, but rather a remote debug mode that allows you to connect to the device using Weinre.
 * - State of the remote debug mode or native debug mode can be changed via Cloud Control or [Rest API](https://developers-staging.signageos.io/api/#tag/DeviceDebug/paths/~1v1~1device~1%7BdeviceUid%7D~1debug/put).
 * :::
 */
class Debug extends events_1.EventEmitter {
    window;
    messagePrefix;
    postMessage;
    static MESSAGE_PREFIX = 'debug';
    static DEBUG_CHANGED = 'debug.changed';
    /** @internal */
    constructor(window, messagePrefix, postMessage) {
        super();
        this.window = window;
        this.messagePrefix = messagePrefix;
        this.postMessage = postMessage;
    }
    /**
     * The `isRemoteDebugEnabled()` method returns the state of the Device debug mode.
     *
     * @returns {Promise<boolean>} Resolves to `true` if the remote debug mode is enabled, otherwise `false`.
     * @since 5.8.0
     */
    async isRemoteDebugEnabled() {
        const { isEnabled } = await this.postMessage({
            type: this.getMessage('is_remote_enabled'),
        });
        return isEnabled;
    }
    /**
     * The `onRemoteDebugChanged()` method sets up a listener, which is called whenever the state of the Device debug mode is changed.
     *
     * @param listener The listener to be called when the state of the remote debug mode is changed.
     * @returns {void} Resolves when the listener is successfully set up.
     * @since 5.8.0
     */
    onRemoteDebugChanged(listener) {
        (0, Validate_1.default)({ listener }).required().function();
        this.addListener(Debug.DEBUG_CHANGED, (data) => listener({ enabled: data.enabled }));
    }
    /** @internal */
    handleMessageData(data) {
        switch (data.type) {
            case this.getMessage('configure'):
                this.emit(Debug.DEBUG_CHANGED, {
                    enabled: data.enabled,
                });
                if (data.enabled) {
                    this.window.WeinreServerURL = data.url;
                    this.window.WeinreServerId = data.id;
                    if (!this.window.__weinreLoaded) {
                        const script = this.window.document.createElement('script');
                        script.src = data.url + '/target/target-script-min.js';
                        this.window.document.body.appendChild(script);
                        this.window.__weinreLoaded = true;
                    }
                }
                else {
                    this.window.WeinreServerURL = undefined;
                    this.window.WeinreServerId = undefined;
                }
                break;
            default:
        }
    }
    getMessage(name) {
        return this.messagePrefix + '.' + Debug.MESSAGE_PREFIX + '.' + name;
    }
}
exports.default = Debug;
//# sourceMappingURL=Debug.js.map