"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GeoFilePicker = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _constants = require("../../../common/constants");
var _geo = require("../../importer/geo");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

class GeoFilePicker extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "_isMounted", false);
    (0, _defineProperty2.default)(this, "state", {
      defaultIndexName: null,
      error: null,
      isLoadingPreview: false,
      importer: null,
      previewSummary: null
    });
    (0, _defineProperty2.default)(this, "_onFileSelect", files => {
      this.props.onClear();
      this.setState({
        defaultIndexName: null,
        error: null,
        isLoadingPreview: false,
        importer: null,
        previewSummary: null
      });
      if (files && files.length) {
        const file = files[0];
        try {
          const importer = (0, _geo.geoImporterFactory)(file);
          this.setState({
            defaultIndexName: file.name.split('.')[0].toLowerCase(),
            importer
          }, this._loadFilePreview);
        } catch (error) {
          this.setState({
            error: error.message
          });
        }
      }
    });
    (0, _defineProperty2.default)(this, "_loadFilePreview", async () => {
      if (!this.state.importer || !this.state.importer.canPreview()) {
        return;
      }
      this.setState({
        isLoadingPreview: true
      });
      let previewError = null;
      let preview = null;
      try {
        preview = await this.state.importer.previewFile(10000, _constants.MB * 3);
        if (preview.features.length === 0) {
          previewError = _i18n.i18n.translate('xpack.fileUpload.geoFilePicker.noFeaturesDetected', {
            defaultMessage: 'No features found in selected file.'
          });
        }
      } catch (error) {
        previewError = error.message;
      }
      if (!this._isMounted) {
        return;
      }
      this.setState({
        error: previewError,
        isLoadingPreview: false,
        previewSummary: !previewError && preview ? _i18n.i18n.translate('xpack.fileUpload.geoFilePicker.previewSummary', {
          defaultMessage: 'Previewing {numFeatures} features, {previewCoverage}% of file.',
          values: {
            numFeatures: preview.features.length,
            previewCoverage: preview.previewCoverage
          }
        }) : null
      });
      if (preview) {
        this.props.onSelect({
          ...preview,
          importer: this.state.importer,
          indexName: this.state.defaultIndexName ? this.state.defaultIndexName : 'features'
        });
      }
    });
  }
  async componentDidMount() {
    this._isMounted = true;
  }
  componentWillUnmount() {
    this._isMounted = false;
  }
  _renderHelpText() {
    return this.state.previewSummary !== null ? this.state.previewSummary : /*#__PURE__*/_react.default.createElement("span", null, _i18n.i18n.translate('xpack.fileUpload.geoFilePicker.acceptedFormats', {
      defaultMessage: 'Formats accepted: {fileTypes}',
      values: {
        fileTypes: _geo.GEO_FILE_TYPES.join(', ')
      }
    }));
  }
  _renderImporterEditor() {
    return this.state.importer ? this.state.importer.renderEditor(this._loadFilePreview) : null;
  }
  render() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, {
      isInvalid: !!this.state.error,
      error: !!this.state.error ? [this.state.error] : [],
      helpText: this._renderHelpText()
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiFilePicker, {
      initialPromptText: _i18n.i18n.translate('xpack.fileUpload.geoFilePicker.filePicker', {
        defaultMessage: 'Select or drag and drop a file'
      }),
      onChange: this._onFileSelect,
      accept: _geo.GEO_FILE_TYPES.join(','),
      isLoading: this.state.isLoadingPreview,
      "data-test-subj": "geoFilePicker"
    })), this._renderImporterEditor());
  }
}
exports.GeoFilePicker = GeoFilePicker;