import _isEmpty from "lodash/fp/isEmpty";
import _constant from "lodash/fp/constant";
import _uniqueId from "lodash/fp/uniqueId";
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); }
import React from 'react';
import PropTypes from 'prop-types';
import { NovaSolidStatusClose as Close, NovaSolidFilesBasicFileUpload2 as FileUploadIcon, NovaSolidFilesBasicFileBlock2 as FileUploadBlockedIcon, NovaSolidFilesBasicFileLines as FileLinesIcon } from '@coorpacademy/nova-icons';
import classnames from 'classnames';
import Loader from '../loader';
import Button from '../button-link';
import getClassState from '../../util/get-class-state';
import style from './style.css';
const constantNull = _constant(null);
class DragAndDrop extends React.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 = _uniqueId('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.createElement("div", {
        className: style.preview
      }, /*#__PURE__*/React.createElement("img", {
        src: previewContent.src
      }));
    } else if (previewContent && previewContent.type === 'video') {
      previewView = /*#__PURE__*/React.createElement("div", {
        className: style.preview
      }, /*#__PURE__*/React.createElement("video", {
        controls: true,
        src: previewContent.src,
        type: "video/*"
      }));
    } else if (previewContent && previewContent.type === 'pdf') {
      previewView = /*#__PURE__*/React.createElement("div", {
        className: style.previewPdf
      }, /*#__PURE__*/React.createElement("p", {
        className: style.previewLabelPdf
      }, previewLabel), /*#__PURE__*/React.createElement(Button, {
        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.createElement("div", {
        className: style.previewXlsxContainer
      }, /*#__PURE__*/React.createElement("div", {
        className: style.previewXlsx
      }, /*#__PURE__*/React.createElement(FileLinesIcon, {
        className: style.iconFile
      })));
    } else if (previewContent && previewContent.type === 'csv') {
      previewView = /*#__PURE__*/React.createElement("div", {
        className: style.preview
      }, /*#__PURE__*/React.createElement("div", {
        className: style.multiplePreview
      }, /*#__PURE__*/React.createElement(FileLinesIcon, {
        className: style.iconFile
      })));
    } else if (loading) {
      previewView = /*#__PURE__*/React.createElement("div", {
        className: style.loaderWrapper
      }, /*#__PURE__*/React.createElement("div", {
        className: style.loadingCancel
      }, /*#__PURE__*/React.createElement(Close, {
        "data-name": "reset-content-icon",
        height: 12,
        width: 12,
        className: style.closeIcon,
        onClick: onReset
      })), /*#__PURE__*/React.createElement("div", {
        className: style.loader
      }, /*#__PURE__*/React.createElement(Loader, {
        theme: "coorpmanager"
      })), /*#__PURE__*/React.createElement("span", {
        className: style.loaderText
      }, "Uploading"));
    } else {
      previewView = /*#__PURE__*/React.createElement("span", null, previewLabel);
    }
    const resetContent = previewContent && previewContent.src && !multiple ? /*#__PURE__*/React.createElement("div", {
      className: classnames(style.resetUploadWrapper, disabled && style.disabled)
    }, /*#__PURE__*/React.createElement("div", {
      className: style.resetSrcLabel
    }, previewContent.label ? previewContent.label : previewContent.src), onReset ? /*#__PURE__*/React.createElement(Close, {
      "data-name": "reset-content-icon",
      height: 12,
      width: 12,
      className: style.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.createElement(Button, _extends({}, defaultButtonProps, {
          label: errorButtonLabel,
          icon: {}
        }));
      } else {
        return /*#__PURE__*/React.createElement(Button, defaultButtonProps);
      }
    };
    const button = buildButton(dragging, error);
    const previewContainer = getClassState(multiple ? style.previewContainerMultiple : style.previewContainer, style.modifiedPreviewContainer, null, modified, error);
    const inputWrapper = getClassState(style.inputWrapper, style.modifiedInputWrapper, style.errorInputWrapper, modified, error);
    const buildContent = () => {
      if (loading) {
        return previewView;
      } else if (!_isEmpty(previewContent)) {
        return /*#__PURE__*/React.createElement("div", {
          className: classnames(previewContainer, disabled && style.disabled)
        }, previewView);
      } else {
        return /*#__PURE__*/React.createElement("div", {
          className: classnames(dragging ? style.dragging : inputWrapper, disabled && style.disabled),
          id: idBox,
          "data-name": "drag-and-drop-box"
        }, /*#__PURE__*/React.createElement("div", {
          className: style.infosContainer
        }, error ? /*#__PURE__*/React.createElement(FileUploadBlockedIcon, {
          className: style.iconError
        }) : /*#__PURE__*/React.createElement(FileUploadIcon, {
          className: style.icon
        }), description ? /*#__PURE__*/React.createElement("div", {
          className: style.description
        }, description) : null, error ? null : /*#__PURE__*/React.createElement("div", {
          className: style.dragAndDropLabel
        }, 'Drag & Drop here')), button, /*#__PURE__*/React.createElement("div", null, children(this.handleDragStart, this.handleDragStop)));
      }
    };
    const content = buildContent();
    return /*#__PURE__*/React.createElement("div", {
      className: style.wrapper,
      "data-name": "drag-and-drop-wrapper"
    }, /*#__PURE__*/React.createElement("div", {
      className: style.title
    }, title), content, error ? /*#__PURE__*/React.createElement("span", {
      className: classnames(style.errorMessage, disabled && style.disabled)
    }, error) : resetContent);
  }
}
DragAndDrop.propTypes = process.env.NODE_ENV !== "production" ? {
  title: PropTypes.string,
  description: PropTypes.string,
  uploadLabel: PropTypes.string,
  previewLabel: PropTypes.string,
  previewContent: PropTypes.shape({
    type: PropTypes.string,
    src: PropTypes.string,
    label: PropTypes.string
  }),
  loading: PropTypes.bool,
  modified: PropTypes.bool,
  disabled: PropTypes.bool,
  children: PropTypes.func,
  onReset: PropTypes.func,
  error: PropTypes.string,
  buttonAriaLabel: PropTypes.string,
  errorButtonLabel: PropTypes.string,
  pdfButtonLabel: PropTypes.string,
  pdfButtonAriaLabel: PropTypes.string,
  multiple: PropTypes.bool
} : {};
export default DragAndDrop;
//# sourceMappingURL=index.js.map