import { useState } from 'react';
import PropTypes from 'prop-types';
import dayjs from '@abtnode/util/lib/dayjs';
import Box from '@mui/material/Box';
import DownloadIcon from '@mui/icons-material/Download';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import Spinner from '@mui/material/CircularProgress';
import Button from '@arcblock/ux/lib/Button';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import Dialog from '@arcblock/ux/lib/Dialog';
import formatName from '@abtnode/util/lib/format-name';
import Toast from '@arcblock/ux/lib/Toast';
import { useNodeContext } from '../contexts/node';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DAYS = [1, 7];
export default function DownloadLog({
  id,
  name = ''
}) {
  const {
    t
  } = useLocaleContext();
  const {
    restApi,
    inService
  } = useNodeContext();
  const [showDialog, setShowDialog] = useState(false);
  const [isDownloading, setIsDownloading] = useState(false);
  const [days, setDays] = useState(1);
  const onDownload = async () => {
    if (isDownloading) {
      return;
    }
    try {
      setIsDownloading(true);
      const logId = inService ? '' : `/${id}`;
      const res = await restApi.get(`/download/log${logId}?days=${days}`, {
        responseType: 'blob',
        timeout: 30 * 60 * 1000
      });
      const href = URL.createObjectURL(res.data);
      const link = document.createElement('a');
      link.href = href;
      link.setAttribute('download', `${formatName(name || id)}.${dayjs().format('YYYYMMDDHHmmss')}.log.zip`);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      URL.revokeObjectURL(href);
      setIsDownloading(false);
      setShowDialog(false);
    } catch (error) {
      setIsDownloading(false);
      Toast.error(error.message);
    }
  };
  const onDaysChange = e => {
    setDays(e.target.value);
  };
  return [/*#__PURE__*/_jsx(Box, {
    style: {
      gap: 16
    },
    sx: {
      display: 'flex',
      alignItems: 'center'
    },
    children: /*#__PURE__*/_jsxs(Button, {
      "data-cy": "download-logs",
      variant: "outlined",
      onClick: () => setShowDialog(true),
      disabled: isDownloading,
      children: [isDownloading ? /*#__PURE__*/_jsx(Spinner, {
        size: 16
      }) : /*#__PURE__*/_jsx(DownloadIcon, {
        fontSize: "small",
        style: {
          transform: 'translateY(2px)'
        }
      }), /*#__PURE__*/_jsx(Box, {
        sx: {
          ml: 0.5
        },
        children: t('common.download')
      })]
    })
  }), showDialog && /*#__PURE__*/_jsx(Dialog, {
    open: true,
    onClose: () => {
      setShowDialog(false);
    },
    disableEscapeKeyDown: true,
    title: t('common.download'),
    PaperProps: {
      style: {
        minHeight: 'auto'
      }
    },
    actions: /*#__PURE__*/_jsxs(Button, {
      "data-cy": "download-logs",
      variant: "outlined",
      onClick: () => onDownload(),
      disabled: isDownloading,
      children: [isDownloading && /*#__PURE__*/_jsx(Spinner, {
        size: 16
      }), /*#__PURE__*/_jsx(Box, {
        sx: {
          ml: 0.5
        },
        children: t('common.confirm')
      })]
    }),
    children: /*#__PURE__*/_jsx(Box, {
      sx: {
        mb: 2,
        display: 'flex',
        justifyContent: 'center'
      },
      children: /*#__PURE__*/_jsx(FormControl, {
        size: "small",
        fullWidth: true,
        children: /*#__PURE__*/_jsx(Select, {
          value: days,
          onChange: onDaysChange,
          children: DAYS.map(x => /*#__PURE__*/_jsx(MenuItem, {
            value: x,
            children: t(`log.lastDays.${x}`)
          }, x))
        })
      })
    })
  })];
}
DownloadLog.propTypes = {
  id: PropTypes.string.isRequired,
  name: PropTypes.string
};