import { useMemo } from 'react';
import PropTypes from 'prop-types';
import { Box, List, Alert, ListItem, ListItemText } from '@mui/material';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import { blueGrey } from '@mui/material/colors';
import AddEndpoint from './add';
import EndpointItem from './item';
import { useSessionContext } from '../../../contexts/session';
import { useBlockletContext } from '../../../contexts/blocklet';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
function ConnectEndpointList({
  releaseId,
  blocklet,
  connectedEndpoints,
  projectId,
  publishedEndpointIds = [],
  onPublish = () => {},
  onDelete = () => {},
  warning = {},
  onOpenConnectEndpoint = () => {}
}) {
  const {
    actions: {
      refreshBlocklet
    }
  } = useBlockletContext();
  const {
    session
  } = useSessionContext();
  const {
    t
  } = useLocaleContext();
  const list = (blocklet?.settings?.endpointList || []).filter(x => x.scope === session?.user?.did);
  const connectedMap = useMemo(() => {
    if (!connectedEndpoints?.length) {
      return {};
    }
    return connectedEndpoints?.reduce((acc, x) => {
      acc[x.endpointId] = x;
      return acc;
    }, {});
  }, [connectedEndpoints]);
  const did = blocklet?.meta?.did;
  const warningList = {};
  Object.entries(warning?.logoErrors || {}).forEach(([key, value]) => {
    const errors = Object.values(value);
    if (errors.length > 0) {
      warningList[key] = errors;
    }
  });
  Object.entries(warning?.screenshotErrors || {}).forEach(([key, value]) => {
    const errors = Object.values(value);
    if (errors.length > 0) {
      warningList[key] = warningList[key] ? [...warningList[key], ...errors] : errors;
    }
  });
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [!!list?.length && /*#__PURE__*/_jsx(Alert, {
      severity: "info",
      style: {
        width: '100%'
      },
      children: /*#__PURE__*/_jsx(Box, {
        sx: {
          color: blueGrey[400]
        },
        children: t('blocklet.publish.endpoint.uploadToEndpoint')
      })
    }), /*#__PURE__*/_jsx(List, {
      sx: {
        flex: 1
      },
      children: list?.map(x => /*#__PURE__*/_jsx(ListItem, {
        "data-cy": "endpoint-switch",
        children: /*#__PURE__*/_jsx(ListItemText, {
          children: /*#__PURE__*/_jsx(EndpointItem, {
            releaseId: releaseId,
            projectId: projectId,
            teamDid: did,
            warningList: warningList[x.url],
            endpoint: x,
            published: publishedEndpointIds?.indexOf(x.id) > -1,
            connectedEndpoint: connectedMap[x.id],
            onPublish: onPublish,
            onDelete: onDelete,
            onOpenConnectEndpoint: onOpenConnectEndpoint
          })
        })
      }, x.url))
    }), /*#__PURE__*/_jsx(AddEndpoint, {
      teamDid: did,
      endpointList: list,
      onAdd: refreshBlocklet
    })]
  });
}
ConnectEndpointList.propTypes = {
  releaseId: PropTypes.string.isRequired,
  blocklet: PropTypes.object.isRequired,
  projectId: PropTypes.string.isRequired,
  warning: PropTypes.object,
  onPublish: PropTypes.func,
  onOpenConnectEndpoint: PropTypes.func,
  onDelete: PropTypes.func,
  connectedEndpoints: PropTypes.arrayOf(PropTypes.object).isRequired,
  publishedEndpointIds: PropTypes.arrayOf(PropTypes.string)
};
export default ConnectEndpointList;