"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * The `sos.deviceInfo` API groups together methods for retrieving information about the device set in the CloudControl, such as location or
 * tags.
 */
class DeviceInfo {
    messagePrefix;
    postMessage;
    static MESSAGE_PREFIX = 'device_info';
    /** @internal */
    constructor(messagePrefix, postMessage) {
        this.messagePrefix = messagePrefix;
        this.postMessage = postMessage;
    }
    /**
     * The `getLocation()` method returns location of the device. Location is requested from server on application start, automatically
     * updated when changed and persisted in local storage.
     *
     * :::note
     * Location can be set only in CloudControl on Device Info page, or via [Rest API](https://developers.signageos.io/api/#tag/DeviceLocation/paths/~1v1~1device~1%7BdeviceUid%7D~1location~1%7BlocationUid%7D/get).
     * :::
     *
     * @returns {Promise<IDeviceLocation | null>} A promise that resolves to the device location or `null` if not set.
     * @since 5.2.0
     */
    async getLocation() {
        const { location } = await this.postMessage({ type: this.getMessage('get_location') });
        return location;
    }
    /**
     * The `getOrganizationTags()` method returns all tags assigned to the device. Tags are requested from server on application start, automatically
     * updated when changed and persisted in local storage.
     *
     * :::note
     * Tags can be set only in CloudControl on Device Info page, or via [Rest API](https://developers.signageos.io/api/#tag/DeviceTag-(Device-Tags)).
     * :::
     *
     * @returns {Promise<IOrganizationTag[]>} A promise that resolves to an array of organization tags.
     * @since 5.2.0
     */
    async getOrganizationTags() {
        const { tags } = await this.postMessage({ type: this.getMessage('get_organization_tags') });
        return tags;
    }
    /**
     * The `getDeviceName()` method returns a name of the device. Name is requested from server on application start, automatically
     * updated when changed and persisted in local storage.
     *
     * :::note
     * Device name can be set only in CloudControl on Device Info page, or via [Rest API](https://developers.signageos.io/api/#tag/Device/paths/~1v2~1device~1%7BdeviceUid%7D/get).
     * :::
     *
     * @returns {Promise<string>} A promise that resolves to the device name.
     * @since 6.6.0
     *
     * @example
     * const deviceName = await sos.deviceInfo.getDeviceName();
     * console.log(`Device name is: ${deviceName}`);
     */
    async getDeviceName() {
        const deviceNameFromTittle = this.geDeviceNameFromTittle();
        if (typeof deviceNameFromTittle === 'string') {
            return deviceNameFromTittle;
        }
        const { name } = await this.postMessage({ type: this.getMessage('get_device_name') });
        return name;
    }
    getMessage(name) {
        return [this.messagePrefix, DeviceInfo.MESSAGE_PREFIX, name].join('.');
    }
    geDeviceNameFromTittle() {
        try {
            const rootDocument = window.parent.document;
            const titleElement = rootDocument.head.getElementsByTagName('title')[0];
            const parentElement = titleElement.parentElement;
            return parentElement.getElementsByTagName('title')[0].innerHTML || null;
        }
        catch {
            return null;
        }
    }
}
exports.default = DeviceInfo;
//# sourceMappingURL=DeviceInfo.js.map