"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SideCarFilePicker = void 0;
exports.getFileNameWithoutExt = getFileNameWithoutExt;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _i18n = require("@kbn/i18n");
var _eui = require("@elastic/eui");
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.
 */

function getFileNameWithoutExt(fileName) {
  const splits = fileName.split('.');
  if (splits.length > 1) {
    splits.pop();
  }
  return splits.join('.');
}
class SideCarFilePicker extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      error: '',
      isInvalid: false
    });
    (0, _defineProperty2.default)(this, "_onSelect", files => {
      if (!files || files.length === 0) {
        this.setState({
          error: '',
          isInvalid: false
        });
        this.props.onSelect(null);
        return;
      }
      const file = files[0];
      if (!this._isSideCarFileValid(file)) {
        this.setState({
          error: this._getSideCarFileNameError(),
          isInvalid: true
        });
        this.props.onSelect(null);
        return;
      }
      this.setState({
        error: '',
        isInvalid: false
      });
      this.props.onSelect(file);
    });
  }
  _isSideCarFileValid(sideCarFile) {
    return getFileNameWithoutExt(this.props.shapefileName) === getFileNameWithoutExt(sideCarFile.name);
  }
  _getSideCarFileNameError() {
    return _i18n.i18n.translate('xpack.fileUpload.shapefile.sideCarFilePicker.error', {
      defaultMessage: '{ext} expected to be {shapefileName}{ext}',
      values: {
        ext: this.props.ext,
        shapefileName: getFileNameWithoutExt(this.props.shapefileName)
      }
    });
  }
  render() {
    return /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, {
      isInvalid: this.state.isInvalid,
      error: this.state.error
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiFilePicker, {
      initialPromptText: _i18n.i18n.translate('xpack.fileUpload.shapefile.sideCarFilePicker.promptText', {
        defaultMessage: `Select '{ext}' file`,
        values: {
          ext: this.props.ext
        }
      }),
      onChange: this._onSelect,
      accept: this.props.ext,
      display: "default",
      isInvalid: this.state.isInvalid,
      "data-test-subj": `shapefileSideCarFilePicker${this.props.ext.replace('.', '_')}`
    }));
  }
}
exports.SideCarFilePicker = SideCarFilePicker;