import type { EventData } from "../../components/app_context.js";
import { t } from "../../services/i18n.js";
import BasicWidget from "../basic_widget.js";
import { Modal } from "bootstrap";
import type { ConfirmDialogCallback } from "./confirm.js";
import { openDialog } from "../../services/dialog.js";
const TPL = /*html*/`
`;
export default class InfoDialog extends BasicWidget {
    private resolve: ConfirmDialogCallback | null;
    private modal!: bootstrap.Modal;
    private $originallyFocused!: JQuery | null;
    private $infoContent!: JQuery;
    private $okButton!: JQuery;
    constructor() {
        super();
        this.resolve = null;
        this.$originallyFocused = null; // element focused before the dialog was opened, so we can return to it afterward
    }
    doRender() {
        this.$widget = $(TPL);
        this.modal = Modal.getOrCreateInstance(this.$widget[0]);
        this.$infoContent = this.$widget.find(".info-dialog-content");
        this.$okButton = this.$widget.find(".info-dialog-ok-button");
        this.$widget.on("shown.bs.modal", () => this.$okButton.trigger("focus"));
        this.$widget.on("hidden.bs.modal", () => {
            if (this.resolve) {
                this.resolve();
            }
            if (this.$originallyFocused) {
                this.$originallyFocused.trigger("focus");
                this.$originallyFocused = null;
            }
        });
        this.$okButton.on("click", () => this.modal.hide());
    }
    showInfoDialogEvent({ message, callback }: EventData<"showInfoDialog">) {
        this.$originallyFocused = $(":focus");
        if (typeof message === "string") {
            this.$infoContent.text(message);
        } else if (Array.isArray(message)) {
            this.$infoContent.html(message[0]);
        } else {
            this.$infoContent.html(message as HTMLElement);
        }
        openDialog(this.$widget);
        this.resolve = callback;
    }
}