"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "EuiFilePicker", {
  enumerable: true,
  get: function get() {
    return _file_picker.EuiFilePicker;
  }
});
Object.defineProperty(exports, "EuiCompressedFilePicker", {
  enumerable: true,
  get: function get() {
    return _file_picker.EuiCompressedFilePicker;
  }
});
exports.OuiCompressedFilePicker = exports.OuiFilePicker = exports.DISPLAYS = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _common = require("../../common");
var _validatable_control = require("../validatable_control");
var _button = require("../../button");
var _progress = require("../../progress");
var _icon = require("../../icon");
var _i18n = require("../../i18n");
var _loading = require("../../loading");
var _accessibility = require("../../../services/accessibility");
var _file_picker = require("../../../eui_components/form/file_picker/file_picker");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) { "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); } return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = (0, _getPrototypeOf2.default)(t); if (r) { var s = (0, _getPrototypeOf2.default)(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return (0, _possibleConstructorReturn2.default)(this, e); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /*
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 *
 * Modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */ /*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var displayToClassNameMap = {
  default: null,
  large: 'ouiFilePicker--large'
};
var DISPLAYS = (0, _common.keysOf)(displayToClassNameMap);
exports.DISPLAYS = DISPLAYS;
var OuiFilePicker = /*#__PURE__*/function (_Component) {
  (0, _inherits2.default)(OuiFilePicker, _Component);
  var _super = _createSuper(OuiFilePicker);
  function OuiFilePicker() {
    var _this;
    (0, _classCallCheck2.default)(this, OuiFilePicker);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      promptText: null,
      isHoveringDrop: false
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fileInput", null);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChange", function (filesSelected) {
      if (!_this.fileInput) return;
      if (_this.fileInput.files && _this.fileInput.files.length > 1) {
        _this.setState({
          promptText: "".concat(_this.fileInput.files.length, " ").concat(filesSelected)
        });
      } else if (_this.fileInput.files && _this.fileInput.files.length === 0) {
        _this.setState({
          promptText: null
        });
      } else {
        _this.setState({
          promptText: _this.fileInput.value.split('\\').pop()
        });
      }
      var onChange = _this.props.onChange;
      if (onChange) {
        onChange(_this.fileInput.files);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "removeFiles", function (e) {
      e.stopPropagation();
      e.preventDefault();
      if (!_this.fileInput) return;
      _this.fileInput.value = '';
      _this.handleChange(null);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "showDrop", function () {
      if (!_this.props.disabled) {
        _this.setState({
          isHoveringDrop: true
        });
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "hideDrop", function () {
      _this.setState({
        isHoveringDrop: false
      });
    });
    return _this;
  }
  (0, _createClass2.default)(OuiFilePicker, [{
    key: "render",
    value: function render() {
      var _this2 = this;
      return /*#__PURE__*/_react.default.createElement(_i18n.OuiI18n, {
        tokens: ['ouiFilePicker.clearSelectedFiles', 'ouiFilePicker.filesSelected'],
        defaults: ['Clear selected files', 'files selected']
      }, function (_ref) {
        var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
          clearSelectedFiles = _ref2[0],
          filesSelected = _ref2[1];
        var _this2$props = _this2.props,
          id = _this2$props.id,
          name = _this2$props.name,
          initialPromptText = _this2$props.initialPromptText,
          className = _this2$props.className,
          disabled = _this2$props.disabled,
          compressed = _this2$props.compressed,
          onChange = _this2$props.onChange,
          isInvalid = _this2$props.isInvalid,
          fullWidth = _this2$props.fullWidth,
          isLoading = _this2$props.isLoading,
          display = _this2$props.display,
          rest = (0, _objectWithoutProperties2.default)(_this2$props, ["id", "name", "initialPromptText", "className", "disabled", "compressed", "onChange", "isInvalid", "fullWidth", "isLoading", "display"]);
        var promptId = (0, _accessibility.htmlIdGenerator)()();
        if (id) {
          promptId = "".concat(id, "-filePicker__prompt");
        }
        var isOverridingInitialPrompt = _this2.state.promptText != null;
        var normalFormControl = display === 'default';
        var classes = (0, _classnames.default)('ouiFilePicker', displayToClassNameMap[display], {
          ouiFilePicker__showDrop: _this2.state.isHoveringDrop,
          'ouiFilePicker--compressed': compressed,
          'ouiFilePicker--fullWidth': fullWidth,
          'ouiFilePicker-isInvalid': isInvalid,
          'ouiFilePicker-isLoading': isLoading,
          'ouiFilePicker-hasFiles': isOverridingInitialPrompt
        }, className);
        var clearButton;
        if (isLoading && normalFormControl) {
          // Override clear button with loading spinner if it is in loading state
          clearButton = /*#__PURE__*/_react.default.createElement(_loading.OuiLoadingSpinner, {
            className: "ouiFilePicker__loadingSpinner"
          });
        } else if (isOverridingInitialPrompt) {
          if (normalFormControl) {
            clearButton = /*#__PURE__*/_react.default.createElement("button", {
              type: "button",
              "aria-label": clearSelectedFiles,
              className: "ouiFilePicker__clearButton",
              onClick: _this2.removeFiles
            }, /*#__PURE__*/_react.default.createElement(_icon.OuiIcon, {
              className: "ouiFilePicker__clearIcon",
              type: "cross"
            }));
          } else {
            clearButton = /*#__PURE__*/_react.default.createElement(_button.OuiButtonEmpty, {
              "aria-label": clearSelectedFiles,
              className: "ouiFilePicker__clearButton",
              size: "xs",
              onClick: _this2.removeFiles
            }, /*#__PURE__*/_react.default.createElement(_i18n.OuiI18n, {
              token: "ouiFilePicker.removeSelected",
              default: "Remove"
            }));
          }
        } else {
          clearButton = null;
        }
        var loader = !normalFormControl && isLoading && /*#__PURE__*/_react.default.createElement(_progress.OuiProgress, {
          size: "xs",
          color: "accent",
          position: "absolute"
        });
        return /*#__PURE__*/_react.default.createElement("div", {
          className: classes
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "ouiFilePicker__wrap"
        }, /*#__PURE__*/_react.default.createElement(_validatable_control.OuiValidatableControl, {
          isInvalid: isInvalid
        }, /*#__PURE__*/_react.default.createElement("input", (0, _extends2.default)({
          type: "file",
          id: id,
          name: name,
          className: "ouiFilePicker__input",
          onChange: function onChange() {
            return _this2.handleChange(filesSelected);
          },
          ref: function ref(input) {
            _this2.fileInput = input;
          },
          onDragOver: _this2.showDrop,
          onDragLeave: _this2.hideDrop,
          onDrop: _this2.hideDrop,
          disabled: disabled,
          "aria-describedby": promptId
        }, rest))), /*#__PURE__*/_react.default.createElement("div", {
          className: "ouiFilePicker__prompt",
          id: promptId
        }, /*#__PURE__*/_react.default.createElement(_icon.OuiIcon, {
          className: "ouiFilePicker__icon",
          type: "importAction",
          size: normalFormControl ? 'm' : 'l',
          "aria-hidden": "true"
        }), /*#__PURE__*/_react.default.createElement("div", {
          className: "ouiFilePicker__promptText"
        }, _this2.state.promptText || initialPromptText), clearButton, loader)));
      });
    }
  }]);
  return OuiFilePicker;
}(_react.Component); // @internal
exports.OuiFilePicker = OuiFilePicker;
(0, _defineProperty2.default)(OuiFilePicker, "defaultProps", {
  initialPromptText: 'Select or drag and drop a file',
  compressed: false,
  display: 'large'
});
OuiFilePicker.propTypes = {
  id: _propTypes.default.string,
  name: _propTypes.default.string,
  className: _propTypes.default.string,
  /**
     * The content that appears in the dropzone if no file is attached
     */
  initialPromptText: _propTypes.default.node,
  /**
     * Use as a callback to access the HTML FileList API
     */
  onChange: _propTypes.default.func,
  /**
     * Reduces the size to a typical (compressed) input
     */
  compressed: _propTypes.default.bool,
  /**
     * Size or type of display;
     * `default` for normal height, similar to other controls;
     * `large` for taller size
     */
  display: _propTypes.default.oneOf(["default", "large"]),
  fullWidth: _propTypes.default.bool,
  isInvalid: _propTypes.default.bool,
  isLoading: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  "aria-label": _propTypes.default.string,
  "data-test-subj": _propTypes.default.string
};
// @internal
var OuiCompressedFilePicker = /*#__PURE__*/function (_OuiFilePicker) {
  (0, _inherits2.default)(OuiCompressedFilePicker, _OuiFilePicker);
  var _super2 = _createSuper(OuiCompressedFilePicker);
  function OuiCompressedFilePicker() {
    (0, _classCallCheck2.default)(this, OuiCompressedFilePicker);
    return _super2.apply(this, arguments);
  }
  return OuiCompressedFilePicker;
}(OuiFilePicker);
/* OUI -> EUI Aliases: Build-Time */
exports.OuiCompressedFilePicker = OuiCompressedFilePicker;
(0, _defineProperty2.default)(OuiCompressedFilePicker, "defaultProps", _objectSpread(_objectSpread({}, OuiFilePicker.defaultProps), {}, {
  compressed: true
}));
/* OUI -> EUI Aliases: Build-Time */