import { FEAGlobals } from "../../../../../common";
import { getToolbarMonitor } from "../../../../hooks/useNotifications";
export const promisifiedGetMonitor = () => new Promise((resolve) => {
    FEAGlobals.finsembleWindow.getMonitor((monitor) => {
        resolve(monitor);
    });
});
export const dockToToolbarRight = async () => {
    const monitorInfo = await getToolbarMonitor();
    const { data } = await FEAGlobals.finsembleWindow.getBounds();
    if (data && monitorInfo) {
        const position = {
            left: monitorInfo.availableRect.right - data.width,
            top: monitorInfo.availableRect.top,
            height: monitorInfo.availableRect.height,
            width: data.width,
        };
        await FEAGlobals.finsembleWindow.setBounds({ bounds: position });
    }
};
export const isValidPosition = (bounds) => bounds &&
    typeof bounds.left !== "undefined" &&
    typeof bounds.top !== "undefined" &&
    typeof bounds.height !== "undefined" &&
    typeof bounds.width !== "undefined";
export const addDockedClasses = () => {
    document.body.classList.add("docked");
};
export const removeDockedClasses = () => {
    document.body.classList.remove("docked");
};
/**
 * Set opaque class to html element
 */
export function setOpaqueClassName(isTransparent) {
    const htmlElement = document.getElementsByTagName("html")[0];
    if (isTransparent) {
        if (htmlElement.classList.contains("opaque")) {
            htmlElement.classList.remove("opaque");
        }
    }
    else {
        if (!htmlElement.classList.contains("opaque")) {
            htmlElement.classList.add("opaque");
        }
    }
}
/**
 * Saves the Center position for future Finsemble restarts
 */
export const saveNotificationsCenterWindowState = ({ showCenter, isDocked, position }) => {
    FEAGlobals.FSBL.Clients.StorageClient.save({
        topic: "finsemble.notifications",
        key: "last.position",
        value: {
            showCenter,
            isDocked,
            position,
        },
    });
};
/**
 * Sets the position of the notifcation center when it gets undocked
 */
export const setUndockedPosition = async (centerState) => {
    const monitorInfo = await promisifiedGetMonitor();
    let { position } = centerState;
    if (!position || !isValidPosition(position)) {
        position = undefined;
        const { data } = await FEAGlobals.finsembleWindow.getBounds();
        if (data) {
            position = {
                left: data.left - 25,
                top: data.top + 25,
                height: Math.round(monitorInfo.availableRect.height * 0.8),
                width: data.width,
            };
        }
    }
    if (position) {
        await FEAGlobals.finsembleWindow.setBounds(position);
    }
};
//# sourceMappingURL=NotificationCenterUtils.js.map