"use strict";

exports.__esModule = true;
exports.default = exports.ShareStatusProvider = exports.ShareFeedback = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _link = _interopRequireDefault(require("../../atom/link"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const SHARE_STATUS = {
  IDLE: 'IDLE',
  ERROR: 'ERROR',
  SUCCESS: 'SUCCESS'
};
const copyTextViaExecCommand = text => {
  const textarea = document.createElement('textarea');
  textarea.style.border = '0';
  textarea.style.padding = '0';
  textarea.style.visibility = 'hidden';
  textarea.style.position = 'absolute';
  textarea.setAttribute('readonly', '');
  textarea.value = text;

  // Element.append is not supported on IE11 :/
  // eslint-disable-next-line unicorn/prefer-node-append
  document.body.appendChild(textarea);
  let status = SHARE_STATUS.IDLE;
  try {
    textarea.select();
    textarea.setSelectionRange(0, textarea.value.length);
    document.execCommand('copy');
    status = SHARE_STATUS.SUCCESS;
  } catch (error) {
    status = SHARE_STATUS.ERROR;
  }

  // Element.remove is not supported on IE11 :/
  // eslint-disable-next-line unicorn/prefer-node-remove
  document.body.removeChild(textarea);
  return status;
};
const StatusContext = /*#__PURE__*/_react.default.createContext(SHARE_STATUS.IDLE);
const ShareStatusProvider = ({
  children
}) => {
  const [status, setStatus] = (0, _react.useState)(SHARE_STATUS.IDLE);
  (0, _react.useEffect)(() => {
    let timeoutId;
    const DURATION = 3000;
    if (status !== SHARE_STATUS.IDLE) {
      timeoutId = setTimeout(() => setStatus(SHARE_STATUS.IDLE), DURATION);
    }
    return () => clearTimeout(timeoutId);
  }, [status]);
  const value = (0, _react.useMemo)(() => [status, setStatus], [status]);
  return /*#__PURE__*/_react.default.createElement(StatusContext.Provider, {
    value: value
  }, children);
};
exports.ShareStatusProvider = ShareStatusProvider;
const ShareFeedback = ({
  successWording,
  errorWording
}) => {
  const [status] = (0, _react.useContext)(StatusContext);
  if (status === SHARE_STATUS.IDLE) return null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.feedback, status === SHARE_STATUS.ERROR && _style.default.errorFeedback)
  }, status === SHARE_STATUS.SUCCESS ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck, {
    className: _style.default.checkIcon,
    width: 13,
    height: 13
  }) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyAttention, {
    className: _style.default.checkIcon,
    width: 13,
    height: 13
  }), status === SHARE_STATUS.SUCCESS ? /*#__PURE__*/_react.default.createElement("p", {
    role: "status"
  }, successWording) : /*#__PURE__*/_react.default.createElement("p", {
    role: "alert"
  }, errorWording));
};
exports.ShareFeedback = ShareFeedback;
const Share = ({
  style,
  text,
  wording
}) => {
  const [, setStatus] = (0, _react.useContext)(StatusContext);
  const onClick = (0, _react.useCallback)(async () => {
    const browserSupportsClipboardWriteText = 'clipboard' in navigator && 'writeText' in navigator.clipboard;
    if (browserSupportsClipboardWriteText) {
      try {
        await navigator.clipboard.writeText(text);
        setStatus(SHARE_STATUS.SUCCESS);
      } catch (error) {
        setStatus(SHARE_STATUS.ERROR);
      }
      return;
    } else {
      const copyStatus = copyTextViaExecCommand(text);
      setStatus(copyStatus);
    }
    return;
  }, [setStatus, text]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (_style.default.container, style)
  }, /*#__PURE__*/_react.default.createElement(_link.default, {
    onClick: onClick,
    className: _style.default.cta,
    "data-name": 'share-button'
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.wrapper
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyShare, {
    className: _style.default.shareIcon,
    width: 18,
    height: 18
  }), /*#__PURE__*/_react.default.createElement("p", null, wording))));
};
ShareStatusProvider.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes.default.node
} : {};
ShareFeedback.propTypes = process.env.NODE_ENV !== "production" ? {
  successWording: _propTypes.default.string,
  errorWording: _propTypes.default.string
} : {};
Share.propTypes = process.env.NODE_ENV !== "production" ? {
  style: _propTypes.default.string,
  text: _propTypes.default.string,
  wording: _propTypes.default.string
} : {};
var _default = exports.default = Share;
//# sourceMappingURL=index.js.map