'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
var getFileIcon = require('./utils/getFileIcon.js');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var base$2 = require('./variations/_disabled/base.js');
var File_styles = require('./File.styles.js');
var File_tokens = require('./File.tokens.js');
var extractExtension = require('../Attach/utils/extractExtension.js');
var Cell = require('./ui/Cell/Cell.js');
var Loader_styles = require('./ui/Loader/Loader.styles.js');
var EmbedIconButton_styles = require('./ui/EmbedIconButton/EmbedIconButton.styles.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var _excluded = ["className", "filename", "description", "actionContent", "actionPlacement", "fileUrl", "thumbUrl", "thumbIcon", "loader", "isLoading", "loaderType", "loaderValue", "loaderMaxValue", "view", "size", "disabled", "onCancel", "onActionClick"];
var fileRoot = function fileRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (props, ref) {
    var _ProgressBarCircularU, _EmbedIconButtonUI, _FileThumb, _FileThumb2;
    var className = props.className,
      filename = props.filename,
      description = props.description,
      actionContent = props.actionContent,
      _props$actionPlacemen = props.actionPlacement,
      actionPlacement = _props$actionPlacemen === void 0 ? 'right' : _props$actionPlacemen,
      fileUrl = props.fileUrl,
      thumbUrl = props.thumbUrl,
      thumbIcon = props.thumbIcon,
      loader = props.loader,
      _props$isLoading = props.isLoading,
      isLoading = _props$isLoading === void 0 ? false : _props$isLoading,
      _props$loaderType = props.loaderType,
      loaderType = _props$loaderType === void 0 ? 'circular' : _props$loaderType,
      _props$loaderValue = props.loaderValue,
      loaderValue = _props$loaderValue === void 0 ? 0 : _props$loaderValue,
      _props$loaderMaxValue = props.loaderMaxValue,
      loaderMaxValue = _props$loaderMaxValue === void 0 ? 100 : _props$loaderMaxValue,
      view = props.view,
      size = props.size,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      onCancel = props.onCancel,
      onActionClick = props.onActionClick,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(props, _excluded);
    var _useMemo = React.useMemo(function () {
        var ext = filename.match(/\./) ? extractExtension.extractExtension(filename) : '';
        var name = ext ? filename.slice(0, -1 - ext.length) : filename;
        return {
          extension: ext || '',
          filenameWithoutExtension: name
        };
      }, [filename]),
      extension = _useMemo.extension,
      filenameWithoutExtension = _useMemo.filenameWithoutExtension;
    var renderAction = function renderAction() {
      if (isLoading && loaderType === 'circular') {
        return loader || _ProgressBarCircularU || (_ProgressBarCircularU = /*#__PURE__*/React__default.default.createElement(Loader_styles.ProgressBarCircularUI, {
          value: loaderValue,
          maxValue: loaderMaxValue
        }, /*#__PURE__*/React__default.default.createElement(EmbedIconButton_styles.EmbedIconButtonUI, {
          onClick: onCancel,
          type: "button",
          "aria-label": "Cancel"
        }, actionContent)));
      }
      if (actionContent) {
        return _EmbedIconButtonUI || (_EmbedIconButtonUI = /*#__PURE__*/React__default.default.createElement(EmbedIconButton_styles.EmbedIconButtonUI, {
          onClick: onActionClick
        }, actionContent));
      }
      return null;
    };
    var renderFileThumb = function renderFileThumb() {
      if (actionPlacement === 'left') {
        return renderAction();
      }
      if (thumbUrl) {
        return _FileThumb || (_FileThumb = /*#__PURE__*/React__default.default.createElement(File_styles.FileThumb, null, /*#__PURE__*/React__default.default.createElement(File_styles.ThumbImg, {
          src: thumbUrl,
          alt: ""
        })));
      }
      if (thumbIcon) {
        return _FileThumb2 || (_FileThumb2 = /*#__PURE__*/React__default.default.createElement(File_styles.FileThumb, null, thumbIcon));
      }
      if (fileUrl) {
        return /*#__PURE__*/React__default.default.createElement(File_styles.FileThumb, null, getFileIcon.getFileIcon(extension, size));
      }
      return null;
    };
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: ref,
      className: cls__default.default(className, File_tokens.classes.fileItem, _rollupPluginBabelHelpers.defineProperty({}, File_tokens.classes.fileLoaderLinear, loaderType === 'linear')),
      view: view,
      size: size,
      disabled: disabled
    }, rest), /*#__PURE__*/React__default.default.createElement(Cell.CellUI, {
      className: cls__default.default(_rollupPluginBabelHelpers.defineProperty({}, File_tokens.classes.fileActionLeft, actionPlacement === 'left')),
      filenameWithoutExtension: filenameWithoutExtension,
      extension: extension ? ".".concat(extension) : '',
      description: description,
      size: size,
      cellContentLeft: renderFileThumb(),
      cellContentRight: actionPlacement === 'right' ? renderAction() : null
    }), isLoading && loaderType === 'linear' && /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, loader || /*#__PURE__*/React__default.default.createElement(Loader_styles.ProgressUI, {
      displayValue: false,
      value: loaderValue
    })));
  });
};
var fileConfig = {
  name: 'File',
  tag: 'div',
  layout: fileRoot,
  base: File_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    },
    disabled: {
      css: base$2.base,
      attrs: true
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

exports.fileConfig = fileConfig;
exports.fileRoot = fileRoot;
