'use strict';

var chunkMVQKV5BJ_cjs = require('./chunk-MVQKV5BJ.cjs');
var classnames = require('classnames');
var React = require('react');
var icons = require('@blueprintjs/icons');
var PostalMime = require('postal-mime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var React__default = /*#__PURE__*/_interopDefault(React);
var PostalMime__default = /*#__PURE__*/_interopDefault(PostalMime);

// src/email-viewer/BaseEmailViewer.module.css
var BaseEmailViewer_default = {};

// src/email-viewer/BaseEmailViewer.tsx
function formatAddress(address) {
  if (address.name) {
    return `${address.name} <${address.address}>`;
  }
  return address.address;
}
function formatAddressList(addresses) {
  return addresses.map(formatAddress).join(", ");
}
function BaseEmailViewer({
  email,
  className
}) {
  const rootClassName = classnames__default.default(BaseEmailViewer_default.container, className);
  const bodyContent = React.useMemo(() => {
    if (email.html != null) {
      return /* @__PURE__ */ React__default.default.createElement("div", {
        className: BaseEmailViewer_default.bodyContainer
      }, /* @__PURE__ */ React__default.default.createElement("iframe", {
        className: BaseEmailViewer_default.bodyIframe,
        sandbox: "allow-same-origin",
        srcDoc: email.html,
        title: "Email body"
      }));
    }
    if (email.text != null) {
      return /* @__PURE__ */ React__default.default.createElement("div", {
        className: BaseEmailViewer_default.textBody
      }, email.text);
    }
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: BaseEmailViewer_default.emptyBody
    }, "No content");
  }, [email.html, email.text]);
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: rootClassName
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.header
  }, email.subject != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.subject
  }, email.subject), email.from != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.headerRow
  }, /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerLabel
  }, "From:"), /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerValue
  }, formatAddress(email.from))), email.to.length > 0 && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.headerRow
  }, /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerLabel
  }, "To:"), /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerValue
  }, formatAddressList(email.to))), email.cc.length > 0 && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.headerRow
  }, /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerLabel
  }, "Cc:"), /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerValue
  }, formatAddressList(email.cc))), email.date != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.headerRow
  }, /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerLabel
  }, "Date:"), /* @__PURE__ */ React__default.default.createElement("span", {
    className: BaseEmailViewer_default.headerValue
  }, email.date))), bodyContent);
}
function extractAddresses(addresses) {
  if (addresses == null) {
    return [];
  }
  const result = [];
  for (const addr of addresses) {
    if (addr.address != null) {
      result.push({
        name: addr.name,
        address: addr.address
      });
    } else if (addr.group != null) {
      for (const member of addr.group) {
        result.push({
          name: member.name,
          address: member.address
        });
      }
    }
  }
  return result;
}
function extractSingleAddress(address) {
  if (address == null) {
    return void 0;
  }
  if (address.address != null) {
    return {
      name: address.name,
      address: address.address
    };
  }
  return void 0;
}
async function parseEmailFromResponse(response) {
  const buffer = await response.arrayBuffer();
  const email = await PostalMime__default.default.parse(buffer);
  return {
    subject: email.subject,
    from: extractSingleAddress(email.from),
    to: extractAddresses(email.to),
    cc: extractAddresses(email.cc),
    date: email.date,
    html: email.html ?? void 0,
    text: email.text ?? void 0
  };
}

// src/email-viewer/EmailViewer.tsx
function _extends() {
  return _extends = Object.assign ? Object.assign.bind() : function(n) {
    for (var e = 1; e < arguments.length; e++) {
      var t = arguments[e];
      for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
    }
    return n;
  }, _extends.apply(null, arguments);
}
function EmailViewer({
  media,
  className,
  ...emailViewerProps
}) {
  const {
    data: email,
    loading,
    error
  } = chunkMVQKV5BJ_cjs.useMediaContents(media, parseEmailFromResponse);
  const rootClassName = classnames__default.default(BaseEmailViewer_default.container, className);
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: rootClassName
  }, loading && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.loadingContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Spin, {
    className: BaseEmailViewer_default.spinnerIcon
  }), "Loading\u2026"), error != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: BaseEmailViewer_default.errorContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Error, {
    className: BaseEmailViewer_default.errorIcon
  }), "Failed to load email: ", error.message), email != null && /* @__PURE__ */ React__default.default.createElement(BaseEmailViewer, _extends({
    email
  }, emailViewerProps)));
}

exports.BaseEmailViewer = BaseEmailViewer;
exports.EmailViewer = EmailViewer;
//# sourceMappingURL=chunk-OISZU73V.cjs.map
//# sourceMappingURL=chunk-OISZU73V.cjs.map