import { Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, Button, useMediaQuery } from '@mui/material';
import { useTheme } from '@mui/material/styles';
import { createContext, isValidElement, useContext, useMemo, useRef, useState } from 'react';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import PropTypes from 'prop-types';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export function Confirm({
  open = false,
  title = undefined,
  description = undefined,
  handleOk = undefined,
  handleCancel = undefined,
  okText = undefined,
  okButtonProps = undefined,
  cancelText = undefined,
  cancelButtonProps = undefined,
  actions = undefined,
  ...rest
}) {
  const theme = useTheme();
  const fullScreen = useMediaQuery(theme.breakpoints.down('md'));
  const {
    t
  } = useLocaleContext();
  const renderDesc = () => {
    if (/*#__PURE__*/isValidElement(description)) return description;
    return /*#__PURE__*/_jsx(DialogContentText, {
      children: description
    });
  };
  const renderActions = useMemo(() => {
    const baseActions = [/*#__PURE__*/_jsx(Button, {
      color: "inherit",
      variant: "text",
      onClick: handleCancel,
      ...cancelButtonProps,
      children: cancelText || t('common.cancel') || 'Cancel'
    }, "cancel"), /*#__PURE__*/_jsx(Button, {
      color: "primary",
      variant: "contained",
      onClick: handleOk,
      ...okButtonProps,
      children: okText || t('common.confirm') || 'Yes'
    }, "ok")];
    return actions ? actions(baseActions, handleCancel) : baseActions;
  }, [t, okText, cancelText, okButtonProps, cancelButtonProps, actions, handleCancel, handleOk]);
  if (!open) return null;
  return /*#__PURE__*/_jsxs(Dialog, {
    fullScreen: fullScreen,
    onClose: handleCancel,
    maxWidth: "md",
    ...rest,
    open: open,
    children: [title && /*#__PURE__*/_jsx(DialogTitle, {
      variant: "h4",
      children: title
    }), /*#__PURE__*/_jsx(DialogContent, {
      sx: {
        minWidth: {
          xs: 'initial',
          md: 400
        }
      },
      children: renderDesc()
    }), /*#__PURE__*/_jsx(DialogActions, {
      children: renderActions
    })]
  });
}
Confirm.propTypes = {
  open: PropTypes.bool,
  title: PropTypes.string,
  description: PropTypes.string,
  handleOk: PropTypes.func,
  handleCancel: PropTypes.func,
  okText: PropTypes.string,
  okButtonProps: PropTypes.object,
  cancelText: PropTypes.string,
  cancelButtonProps: PropTypes.object,
  actions: PropTypes.func
};
export const ConfirmContext = /*#__PURE__*/createContext({});
export const useConfirm = () => useContext(ConfirmContext);
export function ConfirmProvider({
  children = null
}) {
  const [open, setOpen] = useState(false);
  const [currentOptions, setCurrentOptions] = useState({});
  const resolver = useRef(undefined);
  const handleOk = () => {
    resolver.current?.(true);
    setOpen(false);
  };
  const handleCancel = () => {
    resolver.current?.(false);
    setOpen(false);
  };
  const confirm = options => {
    setCurrentOptions(options);
    setOpen(true);
    return new Promise(resolve => {
      resolver.current = resolve;
    });
  };
  const value = useMemo(() => ({
    open,
    confirm
  }), [open]);
  return /*#__PURE__*/_jsxs(ConfirmContext.Provider, {
    value: value,
    children: [children, /*#__PURE__*/_jsx(Confirm, {
      ...(currentOptions.dialogProps || {}),
      open: open,
      onClose: handleCancel,
      title: currentOptions.title,
      description: currentOptions.description,
      okText: currentOptions.okText,
      okButtonProps: currentOptions.okButtonProps,
      cancelText: currentOptions.cancelText,
      cancelButtonProps: currentOptions.cancelButtonProps,
      handleOk: handleOk,
      handleCancel: handleCancel,
      actions: currentOptions.actions
    })]
  });
}
ConfirmProvider.propTypes = {
  children: PropTypes.node
};