import type {Component} from "vue";
import {Subject} from "rxjs";
import type {ConfirmDialogOptions, DialogControls} from "@/components/dialog/dialog";
import ConfirmDialog from "@/components/dialog/ConfirmDialog.vue";

export namespace Dialogs {

    interface NextDialog {
        component: Component,
        controls: DialogControls,
        props: any
    }

    export const subject = new Subject<NextDialog>()


    export function createDialog(component: Component, controls: DialogControls, props: any) {
        subject.next({component, controls, props})
    }

    export async function confirm(options: ConfirmDialogOptions) {
        return new Promise<boolean | void>((resolve, reject) => {
            const controls: DialogControls = {
                onPositive() {
                    resolve(true)
                },
                onNegative() {
                    resolve(false)
                },
                onDismiss() {
                    resolve()
                }
            }
            const next: NextDialog = {
                component: ConfirmDialog,
                controls,
                props: options
            }
            subject.next(next)
        })
    }
}