/**
 * DevExtreme (esm/__internal/ui/m_dialog.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 _extends from "@babel/runtime/helpers/esm/extends";
import Action from "../../core/action";
import config from "../../core/config";
import devices from "../../core/devices";
import Guid from "../../core/guid";
import $ from "../../core/renderer";
import {
    ensureDefined
} from "../../core/utils/common";
import {
    Deferred
} from "../../core/utils/deferred";
import {
    resetActiveElement
} from "../../core/utils/dom";
import {
    extend
} from "../../core/utils/extend";
import {
    getHeight,
    getWidth
} from "../../core/utils/size";
import {
    isPlainObject
} from "../../core/utils/type";
import {
    value as getViewport
} from "../../core/utils/view_port";
import {
    getWindow
} from "../../core/utils/window";
import eventsEngine from "../../events/core/events_engine";
import messageLocalization from "../../localization/message";
import Popup from "../../ui/popup/ui.popup";
import {
    isFluent
} from "../../ui/themes";
import errors from "../../ui/widget/ui.errors";
var window = getWindow();
var DEFAULT_BUTTON = {
    text: "OK",
    onClick: () => true
};
var DX_DIALOG_CLASSNAME = "dx-dialog";
var DX_DIALOG_WRAPPER_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-wrapper");
var DX_DIALOG_ROOT_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-root");
var DX_DIALOG_CONTENT_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-content");
var DX_DIALOG_MESSAGE_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-message");
var DX_DIALOG_BUTTONS_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-buttons");
var DX_DIALOG_BUTTON_CLASSNAME = "".concat(DX_DIALOG_CLASSNAME, "-button");
var DX_BUTTON_CLASSNAME = "dx-button";
var getApplyButtonConfig = () => {
    if (isFluent()) {
        return {
            stylingMode: "contained",
            type: "default"
        }
    }
    return {}
};
var getCancelButtonConfig = () => {
    if (isFluent()) {
        return {
            stylingMode: "outlined",
            type: "default"
        }
    }
    return {}
};
export var custom = function(options) {
    var _a;
    var deferred = Deferred();
    options = options || {};
    var $element = $("<div>").addClass(DX_DIALOG_CLASSNAME).appendTo(getViewport());
    var isMessageDefined = "message" in options;
    var isMessageHtmlDefined = "messageHtml" in options;
    if (isMessageDefined) {
        errors.log("W1013")
    }
    var messageHtml = String(isMessageHtmlDefined ? options.messageHtml : options.message);
    var messageId = options.title ? null : new Guid;
    var $message = $("<div>").addClass(DX_DIALOG_MESSAGE_CLASSNAME).html(messageHtml).attr("id", messageId);
    var popupToolbarItems = [];
    var popupInstance = new Popup($element, extend({
        title: null !== (_a = options.title) && void 0 !== _a ? _a : "",
        showTitle: ensureDefined(options.showTitle, true),
        dragEnabled: ensureDefined(options.dragEnabled, true),
        height: "auto",
        width: options.width,
        showCloseButton: options.showCloseButton || false,
        ignoreChildEvents: false,
        container: $element,
        visualContainer: window,
        dragAndResizeArea: window,
        onContentReady(args) {
            args.component.$content().addClass(DX_DIALOG_CONTENT_CLASSNAME).append($message);
            if (messageId) {
                args.component.$overlayContent().attr("aria-labelledby", messageId)
            }
        },
        onShowing(e) {
            e.component.bottomToolbar().addClass(DX_DIALOG_BUTTONS_CLASSNAME).find(".".concat(DX_BUTTON_CLASSNAME)).addClass(DX_DIALOG_BUTTON_CLASSNAME);
            resetActiveElement()
        },
        onShown(e) {
            var $firstButton = e.component.bottomToolbar().find(".".concat(DX_BUTTON_CLASSNAME)).first();
            eventsEngine.trigger($firstButton, "focus")
        },
        onHiding() {
            deferred.reject()
        },
        onHidden(_ref) {
            var {
                element: element
            } = _ref;
            $(element).remove()
        },
        animation: {
            show: {
                type: "pop",
                duration: 400
            },
            hide: {
                type: "pop",
                duration: 400,
                to: {
                    opacity: 0,
                    scale: 0
                },
                from: {
                    opacity: 1,
                    scale: 1
                }
            }
        },
        rtlEnabled: config().rtlEnabled,
        position: {
            boundaryOffset: {
                h: 10,
                v: 0
            }
        }
    }, options.popupOptions));
    var buttonOptions = options.buttons || [DEFAULT_BUTTON];
    buttonOptions.forEach(options => {
        var action = new Action(options.onClick, {
            context: popupInstance
        });
        popupToolbarItems.push({
            toolbar: "bottom",
            location: devices.current().android ? "after" : "center",
            widget: "dxButton",
            options: _extends(_extends({}, options), {
                onClick() {
                    var result = action.execute(...arguments);
                    hide(result)
                }
            })
        })
    });
    popupInstance.option("toolbarItems", popupToolbarItems);
    popupInstance.$wrapper().addClass(DX_DIALOG_WRAPPER_CLASSNAME);
    if (options.position) {
        popupInstance.option("position", options.position)
    }
    popupInstance.$wrapper().addClass(DX_DIALOG_ROOT_CLASSNAME);

    function hide(value) {
        deferred.resolve(value);
        popupInstance.hide()
    }
    return {
        show: function() {
            if ("phone" === devices.real().deviceType) {
                var isPortrait = getHeight(window) > getWidth(window);
                var width = isPortrait ? "90%" : "60%";
                popupInstance.option({
                    width: width
                })
            }
            popupInstance.show();
            return deferred.promise()
        },
        hide: hide
    }
};
export var alert = function(messageHtml) {
    var title = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "";
    var showTitle = arguments.length > 2 ? arguments[2] : void 0;
    var options = isPlainObject(messageHtml) ? messageHtml : {
        title: title,
        messageHtml: messageHtml,
        showTitle: showTitle,
        buttons: [_extends(_extends({}, DEFAULT_BUTTON), getApplyButtonConfig())],
        dragEnabled: showTitle
    };
    return custom(options).show()
};
export var confirm = function(messageHtml) {
    var title = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "";
    var showTitle = arguments.length > 2 ? arguments[2] : void 0;
    var options = isPlainObject(messageHtml) ? messageHtml : {
        title: title,
        messageHtml: messageHtml,
        showTitle: showTitle,
        buttons: [_extends({
            text: messageLocalization.format("Yes"),
            onClick: () => true
        }, getApplyButtonConfig()), _extends({
            text: messageLocalization.format("No"),
            onClick: () => false
        }, getCancelButtonConfig())],
        dragEnabled: showTitle
    };
    return custom(options).show()
};
