"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.COLORS_MAP = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Text = _interopRequireDefault(require("../text/Text"));

var _Link = _interopRequireDefault(require("../text/Link"));

var _Icon = _interopRequireDefault(require("../icons/Icon"));

var _Spinner = _interopRequireDefault(require("../spinner/Spinner"));

var _excluded = ["children", "color", "iconType", "thumbnailSrc", "src", "loading", "onClose", "onClick", "textRef", "className", "ariaCloseButtonLabel", "statusLabel"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var COLORS_MAP = {
  'gray-20': 'gray-20',
  white: 'white'
};
exports.COLORS_MAP = COLORS_MAP;

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 = (0, _classnames.default)('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.default, {
    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.default, {
    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.default, _extends({}, clickProps, {
    size: "small",
    color: "text-black",
    as: asLink,
    className: "sg-file-handler__link"
  }), children) : /*#__PURE__*/React.createElement(_Text.default, {
    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.default, {
    type: "close",
    size: 16,
    color: "icon-black"
  })));
};

var _default = FileHandler;
exports.default = _default;