import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["children", "color", "iconType", "thumbnailSrc", "src", "loading", "onClose", "onClick", "textRef", "className", "ariaCloseButtonLabel", "statusLabel"];
import * as React from 'react';
import classNames from 'classnames';
import Text from '../text/Text';
import Link from '../text/Link';
import Icon from '../icons/Icon';
import Spinner from '../spinner/Spinner';
export var COLORS_MAP = {
  'gray-20': 'gray-20',
  white: 'white'
};

var FileHandler = function FileHandler(_ref) {
  var children = _ref.children,
      _ref$color = _ref.color,
      color = _ref$color === void 0 ? 'gray-20' : _ref$color,
      _ref$iconType = _ref.iconType,
      iconType = _ref$iconType === void 0 ? 'attachment' : _ref$iconType,
      thumbnailSrc = _ref.thumbnailSrc,
      src = _ref.src,
      _ref$loading = _ref.loading,
      loading = _ref$loading === void 0 ? false : _ref$loading,
      onClose = _ref.onClose,
      onClick = _ref.onClick,
      textRef = _ref.textRef,
      className = _ref.className,
      _ref$ariaCloseButtonL = _ref.ariaCloseButtonLabel,
      ariaCloseButtonLabel = _ref$ariaCloseButtonL === void 0 ? 'Close' : _ref$ariaCloseButtonL,
      statusLabel = _ref.statusLabel,
      props = _objectWithoutProperties(_ref, _excluded);

  var fileHandlerClass = classNames('sg-file-handler', _defineProperty({
    'sg-file-handler--closable': onClose
  }, "sg-file-handler--".concat(COLORS_MAP[color]), color), className);
  var isActionProvided = src !== undefined || onClick;
  var clickProps = onClick ? {
    onClick: onClick
  } : {
    href: src,
    target: '_blank',
    rel: 'noopener noreferrer'
  };
  var role = clickProps.onClick && 'button';
  var asLink = clickProps.onClick ? 'button' : 'a';
  var thumbnail = thumbnailSrc !== undefined ? /*#__PURE__*/React.createElement("img", {
    src: thumbnailSrc,
    alt: "",
    className: "cursor-pointer"
  }) : /*#__PURE__*/React.createElement(Icon, {
    type: iconType,
    size: 24,
    color: "icon-black"
  });
  var interactiveThumbnail = isActionProvided ? /*#__PURE__*/React.createElement("a", _extends({}, clickProps, {
    role: role,
    tabIndex: 0,
    "aria-hidden": true,
    className: "sg-file-handler__link"
  }), thumbnail) : thumbnail;
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: fileHandlerClass
  }), /*#__PURE__*/React.createElement("div", {
    className: "sg-file-handler__icon"
  }, loading ? /*#__PURE__*/React.createElement(Spinner, {
    size: "xsmall",
    "aria-label": (statusLabel === null || statusLabel === void 0 ? void 0 : statusLabel.loading) || 'uploading'
  }) : interactiveThumbnail), /*#__PURE__*/React.createElement("span", {
    className: "sg-file-handler__text",
    ref: textRef
  }, /*#__PURE__*/React.createElement("span", {
    className: "sg-visually-hidden",
    "aria-live": "polite"
  }, !loading && ((statusLabel === null || statusLabel === void 0 ? void 0 : statusLabel.uploaded) || 'uploaded')), isActionProvided ? /*#__PURE__*/React.createElement(Link, _extends({}, clickProps, {
    size: "small",
    color: "text-black",
    as: asLink,
    className: "sg-file-handler__link"
  }), children) : /*#__PURE__*/React.createElement(Text, {
    size: "small",
    weight: "bold"
  }, children)), onClose && /*#__PURE__*/React.createElement("button", {
    className: "sg-file-handler__close-button",
    onClick: onClose,
    "aria-label": ariaCloseButtonLabel
  }, /*#__PURE__*/React.createElement(Icon, {
    type: "close",
    size: 16,
    color: "icon-black"
  })));
};

export default FileHandler;