/* eslint-disable react/jsx-one-expression-per-line */
import { useContext } from 'react';
import styled from '@emotion/styled';
import CircularProgress from '@mui/material/CircularProgress';
import Typography from '@mui/material/Typography';
import Link from '@mui/material/Link';
import InfoRow from '@arcblock/ux/lib/InfoRow';
import { LocaleContext } from '@arcblock/ux/lib/Locale/context';
import { filesize } from '../util';
import useBlockletWithDiskInfo from '../hooks/use-bocklet-with-disk-info';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function BlockletDiskInfo({
  ...rest
}) {
  const {
    t
  } = useContext(LocaleContext);
  // 此处应该使用自定义的 hook 来获取数据为最优解。一是 refreshBlocklet 在其他地方频繁调用不会影响这里的数据准确性，二是 disk info 数据的生命周期只在这个页面
  const {
    blocklet,
    loading
  } = useBlockletWithDiskInfo();
  if (blocklet?.diskInfo) {
    const renderDiskInfo = (dir, size, handler) => /*#__PURE__*/_jsxs(Typography, {
      children: [handler ?
      /*#__PURE__*/
      /* eslint-disable-next-line no-script-url, jsx-a11y/anchor-is-valid */
      _jsx(Link, {
        style: {
          cursor: 'pointer'
        },
        onClick: handler,
        underline: "hover",
        children: dir
      }) : dir, ' ', /*#__PURE__*/_jsx("strong", {
        children: size ? filesize(size) : 'empty'
      })]
    });
    const env = blocklet.environments.reduce((acc, x) => {
      acc[x.key] = x.value;
      return acc;
    }, {});
    const rows = [{
      key: 'appDir',
      name: t('blocklet.diskInfo.appDir'),
      value: renderDiskInfo(env.BLOCKLET_APP_DIR, blocklet.diskInfo.app)
    }, {
      key: 'dataDir',
      name: t('blocklet.diskInfo.dataDir'),
      value: renderDiskInfo(env.BLOCKLET_DATA_DIR, blocklet.diskInfo.data)
    }, {
      key: 'logDir',
      name: t('blocklet.diskInfo.logDir'),
      value: renderDiskInfo(env.BLOCKLET_LOG_DIR, blocklet.diskInfo.log)
    }, {
      key: 'cacheDir',
      name: t('blocklet.diskInfo.cacheDir'),
      value: renderDiskInfo(env.BLOCKLET_CACHE_DIR, blocklet.diskInfo.cache)
    }];
    return /*#__PURE__*/_jsx(Div, {
      component: "div",
      ...rest,
      children: rows.map(row => /*#__PURE__*/_jsx(InfoRow, {
        valueComponent: "div",
        nameWidth: 120,
        name: row.name,
        children: row.value
      }, row.key))
    });
  }
  if (loading) {
    return /*#__PURE__*/_jsx(CircularProgress, {});
  }
  return /*#__PURE__*/_jsx(Typography, {
    children: t('blocklet.diskInfo.empty')
  });
}
const Div = styled(Typography)``;