import { UNOWNED_DID } from '@abtnode/constant';
import Dialog from '@arcblock/ux/lib/Dialog';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import IOSSwitch from '@arcblock/ux/lib/Switch';
import Toast from '@arcblock/ux/lib/Toast/index';
import { Icon } from '@iconify/react';
import { Box, Button, Typography } from '@mui/material';
import Spinner from '@mui/material/CircularProgress';
import PropTypes from 'prop-types';
import { useEffect, useState } from 'react';
import { useNodeContext } from '../../../contexts/node';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
function ProjectSetting({
  did,
  params,
  setParams,
  projectId,
  open,
  onClose
}) {
  const {
    t
  } = useLocaleContext();
  const [loading, setLoading] = useState(false);
  const {
    api
  } = useNodeContext();
  const [autoUpload, setAutoUpload] = useState(false);
  const [possibleSameStore, setPossibleSameStore] = useState(false);
  const handleClose = () => {
    setAutoUpload(params.autoUpload);
    setPossibleSameStore(params.possibleSameStore);
    setLoading(false);
    onClose(false);
  };
  const handleSubmit = async () => {
    setLoading(true);
    try {
      await api.updateProject({
        input: {
          did,
          projectId,
          blockletTitle: params.blockletTitle || '',
          blockletDescription: params.blockletDescription || '',
          blockletIntroduction: params.blockletIntroduction || '',
          autoUpload: !!autoUpload,
          possibleSameStore: !!possibleSameStore
        }
      });
      setParams({
        autoUpload,
        possibleSameStore
      });
      onClose(false);
    } catch (err) {
      Toast.error(err.message);
    }
    setLoading(false);
  };
  useEffect(() => {
    setAutoUpload(params.autoUpload);
    setPossibleSameStore(params.possibleSameStore);
  }, [params]);
  const hasProject = !!(projectId && projectId !== UNOWNED_DID);
  return /*#__PURE__*/_jsx(Dialog, {
    title: t('blocklet.publish.projectSettings'),
    disableEscapeKeyDown: true,
    disablePortal: false,
    fullWidth: true,
    open: open,
    onClose: handleClose,
    actions: /*#__PURE__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/_jsx(Button, {
        onClick: e => {
          e.stopPropagation();
          onClose();
        },
        color: "inherit",
        children: t('common.cancel')
      }), hasProject && /*#__PURE__*/_jsxs(Button, {
        onClick: e => {
          e.stopPropagation();
          handleSubmit();
        },
        color: "primary",
        "data-cy": "install-blocklet-next-step",
        disabled: loading,
        variant: "contained",
        autoFocus: true,
        children: [loading && /*#__PURE__*/_jsx(Spinner, {
          size: 16
        }), t('common.confirm')]
      })]
    }),
    children: !hasProject ? /*#__PURE__*/_jsxs(Box, {
      sx: {
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center'
      },
      children: [/*#__PURE__*/_jsx(Box, {
        component: Icon,
        icon: "ph:empty",
        sx: {
          fontSize: 40,
          mb: 3,
          opacity: 0.6
        }
      }), /*#__PURE__*/_jsx(Typography, {
        variant: "body1",
        sx: {
          flex: 1
        },
        children: t('blocklet.publish.needProjectId')
      })]
    }) : /*#__PURE__*/_jsxs(Box, {
      component: "label",
      sx: {
        display: 'flex',
        flexDirection: 'column',
        gap: 2
      },
      children: [/*#__PURE__*/_jsxs(Box, {
        sx: {
          display: 'flex',
          flexDirection: 'row',
          alignItems: 'center'
        },
        children: [/*#__PURE__*/_jsx(Box, {
          component: Icon,
          icon: "ph:upload",
          sx: {
            fontSize: 24,
            mr: 2
          }
        }), /*#__PURE__*/_jsx(Typography, {
          variant: "body1",
          sx: {
            flex: 1
          },
          children: t('blocklet.publish.autoUploadTip')
        }), /*#__PURE__*/_jsx(IOSSwitch, {
          checked: autoUpload,
          onChange: (_, x) => setAutoUpload(x)
        })]
      }), /*#__PURE__*/_jsxs(Box, {
        component: "label",
        sx: {
          display: 'flex',
          flexDirection: 'row',
          alignItems: 'center'
        },
        children: [/*#__PURE__*/_jsx(Box, {
          component: Icon,
          icon: "fluent-mdl2:merge-duplicate",
          sx: {
            fontSize: 24,
            mr: 2
          }
        }), /*#__PURE__*/_jsx(Typography, {
          variant: "body1",
          sx: {
            flex: 1
          },
          children: t('blocklet.publish.possibleSameStoreTip')
        }), /*#__PURE__*/_jsx(IOSSwitch, {
          checked: possibleSameStore,
          onChange: (_, x) => setPossibleSameStore(x)
        })]
      })]
    })
  });
}
ProjectSetting.propTypes = {
  did: PropTypes.string.isRequired,
  params: PropTypes.object.isRequired,
  open: PropTypes.bool.isRequired,
  onClose: PropTypes.func.isRequired,
  setParams: PropTypes.func.isRequired,
  projectId: PropTypes.string.isRequired
};
export default ProjectSetting;