/**
 * DevExtreme (esm/__internal/ui/m_notify.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../core/renderer";
import {
    extend
} from "../../core/utils/extend";
import {
    isPlainObject,
    isString
} from "../../core/utils/type";
import {
    value as viewPort
} from "../../core/utils/view_port";
import {
    getWindow
} from "../../core/utils/window";
import Toast from "../../ui/toast";
var window = getWindow();
var $notify = null;
var $containers = {};

function notify(message, typeOrStack, displayTime) {
    var options = isPlainObject(message) ? message : {
        message: message
    };
    var stack = isPlainObject(typeOrStack) ? typeOrStack : void 0;
    var type = isPlainObject(typeOrStack) ? void 0 : typeOrStack;
    var {
        onHidden: userOnHidden
    } = options;
    if (null === stack || void 0 === stack ? void 0 : stack.position) {
        var {
            position: position
        } = stack;
        var direction = stack.direction || getDefaultDirection(position);
        var containerKey = isString(position) ? position : "".concat(position.top, "-").concat(position.left, "-").concat(position.bottom, "-").concat(position.right);
        var {
            onShowing: userOnShowing
        } = options;
        var $container = getStackContainer(containerKey);
        setContainerClasses($container, direction);
        extend(options, {
            container: $container,
            _skipContentPositioning: true,
            onShowing(args) {
                setContainerStyles($container, direction, position);
                null === userOnShowing || void 0 === userOnShowing ? void 0 : userOnShowing(args)
            }
        })
    }
    extend(options, {
        type: type,
        displayTime: displayTime,
        onHidden(args) {
            $(args.element).remove();
            null === userOnHidden || void 0 === userOnHidden ? void 0 : userOnHidden(args)
        }
    });
    $notify = $("<div>").appendTo(viewPort());
    new Toast($notify, options).show()
}
var getDefaultDirection = position => isString(position) && position.includes("top") ? "down-push" : "up-push";
var createStackContainer = key => {
    var $container = $("<div>").appendTo(viewPort());
    $containers[key] = $container;
    return $container
};
var getStackContainer = key => {
    var $container = $containers[key];
    return $container || createStackContainer(key)
};
var setContainerClasses = (container, direction) => {
    var containerClasses = "dx-toast-stack dx-toast-stack-".concat(direction, "-direction");
    container.removeAttr("class").addClass(containerClasses)
};
var setContainerStyles = (container, direction, position) => {
    var {
        offsetWidth: toastWidth,
        offsetHeight: toastHeight
    } = container.children().first().get(0);
    var dimensions = {
        toastWidth: toastWidth,
        toastHeight: toastHeight,
        windowHeight: window.innerHeight,
        windowWidth: window.innerWidth
    };
    var coordinates = isString(position) ? getCoordinatesByAlias(position, dimensions) : position;
    var styles = getPositionStylesByCoordinates(direction, coordinates, dimensions);
    container.css(styles)
};
var getCoordinatesByAlias = (alias, _ref) => {
    var {
        toastWidth: toastWidth,
        toastHeight: toastHeight,
        windowHeight: windowHeight,
        windowWidth: windowWidth
    } = _ref;
    switch (alias) {
        case "top left":
            return {
                top: 10, left: 10
            };
        case "top right":
            return {
                top: 10, right: 10
            };
        case "bottom left":
            return {
                bottom: 10, left: 10
            };
        case "bottom right":
            return {
                bottom: 10, right: 10
            };
        case "top center":
            return {
                top: 10, left: Math.round(windowWidth / 2 - toastWidth / 2)
            };
        case "left center":
            return {
                top: Math.round(windowHeight / 2 - toastHeight / 2), left: 10
            };
        case "right center":
            return {
                top: Math.round(windowHeight / 2 - toastHeight / 2), right: 10
            };
        case "center":
            return {
                top: Math.round(windowHeight / 2 - toastHeight / 2), left: Math.round(windowWidth / 2 - toastWidth / 2)
            };
        case "bottom center":
        default:
            return {
                bottom: 10, left: Math.round(windowWidth / 2 - toastWidth / 2)
            }
    }
};
var getPositionStylesByCoordinates = (direction, coordinates, dimensions) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    var {
        toastWidth: toastWidth,
        toastHeight: toastHeight,
        windowHeight: windowHeight,
        windowWidth: windowWidth
    } = dimensions;
    switch (direction.replace(/-push|-stack/g, "")) {
        case "up":
            return {
                bottom: null !== (_a = coordinates.bottom) && void 0 !== _a ? _a : windowHeight - toastHeight - coordinates.top, top: "", left: null !== (_b = coordinates.left) && void 0 !== _b ? _b : "", right: null !== (_c = coordinates.right) && void 0 !== _c ? _c : ""
            };
        case "down":
            return {
                top: null !== (_d = coordinates.top) && void 0 !== _d ? _d : windowHeight - toastHeight - coordinates.bottom, bottom: "", left: null !== (_e = coordinates.left) && void 0 !== _e ? _e : "", right: null !== (_f = coordinates.right) && void 0 !== _f ? _f : ""
            };
        case "left":
            return {
                right: null !== (_g = coordinates.right) && void 0 !== _g ? _g : windowWidth - toastWidth - coordinates.left, left: "", top: null !== (_h = coordinates.top) && void 0 !== _h ? _h : "", bottom: null !== (_j = coordinates.bottom) && void 0 !== _j ? _j : ""
            };
        case "right":
            return {
                left: null !== (_k = coordinates.left) && void 0 !== _k ? _k : windowWidth - toastWidth - coordinates.right, right: "", top: null !== (_l = coordinates.top) && void 0 !== _l ? _l : "", bottom: null !== (_m = coordinates.bottom) && void 0 !== _m ? _m : ""
            }
    }
};
export default notify;
