/**
 * Toast notification stack for the product designer.
 */
(function (global) {
    "use strict";

    global.IAPC = global.IAPC || {};

    var iapcToastRoot = null;
    var IAPC_TOAST_MAX_VISIBLE = 3;

    function translate(key) {
        var args = Array.prototype.slice.call(arguments, 1);
        var i18n = global.IAPC_DATA && global.IAPC_DATA.i18n ? global.IAPC_DATA.i18n : {};
        var str = i18n[key] || key;

        args.forEach(function (arg, index) {
            var num = index + 1;
            str = str.replace(new RegExp("%" + num + "\\$[sd]", "g"), String(arg));
        });

        args.forEach(function (arg) {
            str = str.replace(/%[sd]/, String(arg));
        });

        return str;
    }

    function clearToastTimer(toast) {
        if (toast && toast._iapcToastTimer) {
            global.clearTimeout(toast._iapcToastTimer);
            toast._iapcToastTimer = null;
        }
    }

    function ensureToastRoot() {
        if (iapcToastRoot) {
            return iapcToastRoot;
        }

        iapcToastRoot = document.createElement("div");
        iapcToastRoot.className = "iapc-toast-stack";
        iapcToastRoot.setAttribute("aria-live", "polite");
        iapcToastRoot.setAttribute("aria-atomic", "true");
        document.body.appendChild(iapcToastRoot);

        return iapcToastRoot;
    }

    function dismissToast(toast) {
        if (!toast || toast.classList.contains("iapc-toast--hide")) {
            return;
        }

        clearToastTimer(toast);
        toast.classList.remove("iapc-toast--visible");
        toast.classList.add("iapc-toast--hide");

        global.setTimeout(function () {
            toast.remove();
        }, 360);
    }

    function trimToastStack(root) {
        var toasts = root.querySelectorAll(".iapc-toast:not(.iapc-toast--hide)");

        if (toasts.length >= IAPC_TOAST_MAX_VISIBLE) {
            dismissToast(toasts[0]);
        }
    }

    function showToast(toast) {
        global.requestAnimationFrame(function () {
            global.requestAnimationFrame(function () {
                toast.classList.add("iapc-toast--visible");
            });
        });
    }

    function notify(message, type) {
        if (!message) {
            return;
        }

        type = type || "info";

        var root = ensureToastRoot();
        var toast = document.createElement("div");
        toast.className = "iapc-toast iapc-toast--" + type;
        toast.setAttribute("role", type === "error" ? "alert" : "status");

        var icon = document.createElement("span");
        icon.className = "iapc-toast-icon";
        icon.setAttribute("aria-hidden", "true");

        if (type === "success") {
            icon.textContent = "✓";
        } else if (type === "error" || type === "warning") {
            icon.textContent = "!";
        } else {
            icon.textContent = "i";
        }

        var text = document.createElement("p");
        text.className = "iapc-toast-text";
        text.textContent = message;

        var close = document.createElement("button");
        close.type = "button";
        close.className = "iapc-toast-close";
        close.setAttribute("aria-label", translate("closeToast"));
        close.textContent = "×";

        toast.appendChild(icon);
        toast.appendChild(text);
        toast.appendChild(close);

        trimToastStack(root);
        root.appendChild(toast);

        showToast(toast);

        toast._iapcToastTimer = global.setTimeout(function () {
            dismissToast(toast);
        }, type === "error" ? 6500 : 4500);

        close.addEventListener("click", function () {
            clearToastTimer(toast);
            dismissToast(toast);
        });
    }

    global.IAPC.toast = {
        notify: notify
    };
})(window);
