import { Button, Group, Modal, Text } from "@mantine/core";
import { t } from "../i18n";
import { useConfirmStore } from "../utils/confirm";

// 全局确认框宿主：挂一次于 React 树（main.tsx，MantineProvider 内）。confirmDialog() 触发时弹出，
// 用户点确认 / 取消结清对应 Promise。DOM 渲染，浏览器 + Wails 桌面均可用（取代失效的 window.confirm）。
export function ConfirmHost()
{
    const pending = useConfirmStore((s) => s.pending);
    const settle = useConfirmStore((s) => s.settle);

    return (
        <Modal
            opened={pending !== null}
            onClose={() => settle(false)}
            title={t("common.confirm")}
            centered
            withCloseButton={false}
        >
            <Text size="sm">{pending?.message}</Text>
            <Group justify="flex-end" mt="lg">
                <Button variant="default" onClick={() => settle(false)}>
                    {t("common.cancel")}
                </Button>
                <Button color="red" onClick={() => settle(true)}>
                    {t("common.confirm")}
                </Button>
            </Group>
        </Modal>
    );
}
