"use strict";

exports.__esModule = true;
exports.default = void 0;
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _constant2 = _interopRequireDefault(require("lodash/fp/constant"));
var _uniqueId2 = _interopRequireDefault(require("lodash/fp/uniqueId"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _loader = _interopRequireDefault(require("../loader"));
var _buttonLink = _interopRequireDefault(require("../button-link"));
var _getClassState = _interopRequireDefault(require("../../util/get-class-state"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
const constantNull = (0, _constant2.default)(null);
class DragAndDrop extends _react.default.Component {
  constructor(props) {
    super(props);
    this.state = {
      dragging: false
    };
    this.handleDragStart = this.handleDragStart.bind(this);
    this.handleDragStop = this.handleDragStop.bind(this);
  }
  handleDragStart() {
    this.setState({
      dragging: true
    });
  }
  handleDragStop() {
    this.setState({
      dragging: false
    });
  }
  render() {
    const idBox = (0, _uniqueId2.default)('drop-box-');
    const {
      children = constantNull,
      title,
      description,
      uploadLabel,
      previewLabel = '',
      previewContent,
      loading = false,
      modified = false,
      onReset = null,
      error = '',
      buttonAriaLabel = '',
      errorButtonLabel = '',
      disabled = false,
      pdfButtonLabel,
      pdfButtonAriaLabel,
      multiple
    } = this.props;
    const {
      dragging
    } = this.state;
    let previewView = null;
    const customButtonStyle = {
      width: 'auto',
      maxWidth: '150px',
      padding: '12px 24px',
      boxSizing: 'border-box'
    };
    if (previewContent && previewContent.type === 'image') {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.preview
      }, /*#__PURE__*/_react.default.createElement("img", {
        src: previewContent.src
      }));
    } else if (previewContent && previewContent.type === 'video') {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.preview
      }, /*#__PURE__*/_react.default.createElement("video", {
        controls: true,
        src: previewContent.src,
        type: "video/*"
      }));
    } else if (previewContent && previewContent.type === 'pdf') {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.previewPdf
      }, /*#__PURE__*/_react.default.createElement("p", {
        className: _style.default.previewLabelPdf
      }, previewLabel), /*#__PURE__*/_react.default.createElement(_buttonLink.default, {
        type: "secondary",
        link: {
          href: previewContent.src,
          target: '_blank',
          download: false
        },
        label: pdfButtonLabel,
        "aria-label": pdfButtonAriaLabel,
        "data-name": "default-button-pdf",
        icon: {
          position: 'left',
          type: 'pdf'
        },
        customStyle: customButtonStyle
      }));
    } else if (previewContent && previewContent.type === 'xlsx') {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.previewXlsxContainer
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.previewXlsx
      }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidFilesBasicFileLines, {
        className: _style.default.iconFile
      })));
    } else if (previewContent && previewContent.type === 'csv') {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.preview
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.multiplePreview
      }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidFilesBasicFileLines, {
        className: _style.default.iconFile
      })));
    } else if (loading) {
      previewView = /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.loaderWrapper
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.loadingCancel
      }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidStatusClose, {
        "data-name": "reset-content-icon",
        height: 12,
        width: 12,
        className: _style.default.closeIcon,
        onClick: onReset
      })), /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.loader
      }, /*#__PURE__*/_react.default.createElement(_loader.default, {
        theme: "coorpmanager"
      })), /*#__PURE__*/_react.default.createElement("span", {
        className: _style.default.loaderText
      }, "Uploading"));
    } else {
      previewView = /*#__PURE__*/_react.default.createElement("span", null, previewLabel);
    }
    const resetContent = previewContent && previewContent.src && !multiple ? /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)(_style.default.resetUploadWrapper, disabled && _style.default.disabled)
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.resetSrcLabel
    }, previewContent.label ? previewContent.label : previewContent.src), onReset ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidStatusClose, {
      "data-name": "reset-content-icon",
      height: 12,
      width: 12,
      className: _style.default.closeIcon,
      onClick: onReset
    }) : null) : null;
    const buildButton = () => {
      const defaultButtonProps = {
        label: uploadLabel,
        'aria-label': buttonAriaLabel,
        'data-name': 'default-button',
        icon: {
          position: 'left',
          type: 'folders'
        },
        customStyle: customButtonStyle
      };
      if (dragging) {
        return null;
      } else if (error) {
        return /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, defaultButtonProps, {
          label: errorButtonLabel,
          icon: {}
        }));
      } else {
        return /*#__PURE__*/_react.default.createElement(_buttonLink.default, defaultButtonProps);
      }
    };
    const button = buildButton(dragging, error);
    const previewContainer = (0, _getClassState.default)(multiple ? _style.default.previewContainerMultiple : _style.default.previewContainer, _style.default.modifiedPreviewContainer, null, modified, error);
    const inputWrapper = (0, _getClassState.default)(_style.default.inputWrapper, _style.default.modifiedInputWrapper, _style.default.errorInputWrapper, modified, error);
    const buildContent = () => {
      if (loading) {
        return previewView;
      } else if (!(0, _isEmpty2.default)(previewContent)) {
        return /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)(previewContainer, disabled && _style.default.disabled)
        }, previewView);
      } else {
        return /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)(dragging ? _style.default.dragging : inputWrapper, disabled && _style.default.disabled),
          id: idBox,
          "data-name": "drag-and-drop-box"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: _style.default.infosContainer
        }, error ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidFilesBasicFileBlock2, {
          className: _style.default.iconError
        }) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidFilesBasicFileUpload2, {
          className: _style.default.icon
        }), description ? /*#__PURE__*/_react.default.createElement("div", {
          className: _style.default.description
        }, description) : null, error ? null : /*#__PURE__*/_react.default.createElement("div", {
          className: _style.default.dragAndDropLabel
        }, 'Drag & Drop here')), button, /*#__PURE__*/_react.default.createElement("div", null, children(this.handleDragStart, this.handleDragStop)));
      }
    };
    const content = buildContent();
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.wrapper,
      "data-name": "drag-and-drop-wrapper"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.title
    }, title), content, error ? /*#__PURE__*/_react.default.createElement("span", {
      className: (0, _classnames.default)(_style.default.errorMessage, disabled && _style.default.disabled)
    }, error) : resetContent);
  }
}
DragAndDrop.propTypes = process.env.NODE_ENV !== "production" ? {
  title: _propTypes.default.string,
  description: _propTypes.default.string,
  uploadLabel: _propTypes.default.string,
  previewLabel: _propTypes.default.string,
  previewContent: _propTypes.default.shape({
    type: _propTypes.default.string,
    src: _propTypes.default.string,
    label: _propTypes.default.string
  }),
  loading: _propTypes.default.bool,
  modified: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  children: _propTypes.default.func,
  onReset: _propTypes.default.func,
  error: _propTypes.default.string,
  buttonAriaLabel: _propTypes.default.string,
  errorButtonLabel: _propTypes.default.string,
  pdfButtonLabel: _propTypes.default.string,
  pdfButtonAriaLabel: _propTypes.default.string,
  multiple: _propTypes.default.bool
} : {};
var _default = exports.default = DragAndDrop;
//# sourceMappingURL=index.js.map