import { createElement, Fragment } from 'react';
import PropTypes from 'prop-types';
import Tooltip from '@mui/material/Tooltip';
import PrivacyTipIcon from '@mui/icons-material/PrivacyTip';
import DnsOutlinedIcon from '@mui/icons-material/DnsOutlined';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import VerifiedDot from '../../dot';
import { DomainType } from '../types';
import { shouldCheckDomainStatus, checkIsWildcardDomain } from '../../util';
import Certificate from '../../certificate';
import { jsx as _jsx } from "react/jsx-runtime";
export default function DomainStatus({
  domain,
  filters = ['domain', 'http']
}) {
  const {
    t
  } = useLocaleContext();
  const {
    value: name,
    domainStatus
  } = domain;
  if (!shouldCheckDomainStatus(name)) {
    return null;
  }
  let domainIcon = null;
  let httpsIcon = null;
  const {
    isHttps,
    matchedCert
  } = domainStatus;
  const {
    dns: {
      ip,
      isDnsResolved,
      isCnameMatch
    } = {}
  } = domainStatus;
  const resolved = isDnsResolved && isCnameMatch;
  if (!checkIsWildcardDomain(name)) {
    if (resolved || ip === '127.0.0.1') {
      domainIcon = /*#__PURE__*/_jsx(Tooltip, {
        title: t('router.domain.verify.ok'),
        children: /*#__PURE__*/_jsx(VerifiedDot, {
          color: "success"
        })
      });
    } else {
      domainIcon = /*#__PURE__*/_jsx(Tooltip, {
        title: t('router.domain.verify.notResolved'),
        children: /*#__PURE__*/_jsx(VerifiedDot, {
          color: "error"
        })
      });
    }
  }
  if (resolved) {
    if (isHttps) {
      httpsIcon = /*#__PURE__*/_jsx(Certificate, {
        certificate: matchedCert
      });
    } else {
      httpsIcon = /*#__PURE__*/_jsx(Tooltip, {
        title: t('router.domain.https.bad'),
        children: /*#__PURE__*/_jsx(PrivacyTipIcon, {
          style: {
            color: '#D0021B',
            fontSize: '1em',
            cursor: 'pointer'
          }
        })
      });
    }
  } else {
    httpsIcon = /*#__PURE__*/_jsx(Tooltip, {
      title: t('router.domain.verify.notResolved'),
      children: /*#__PURE__*/_jsx(DnsOutlinedIcon, {
        style: {
          color: '#D0021B',
          fontSize: '1em',
          cursor: 'pointer'
        }
      })
    });
  }
  const items = filters.map(x => {
    if (x === 'domain') {
      return domainIcon;
    }
    if (x === 'http') {
      return httpsIcon;
    }
    return null;
  });
  return /*#__PURE__*/createElement(Fragment, null, ...items);
}
DomainStatus.propTypes = {
  domain: DomainType.isRequired,
  filters: PropTypes.array
};