"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _antd = require("antd");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var InputGroup = _antd.Input.Group;

var CustomTreeSelect = function CustomTreeSelect(props) {
  var _props$value = props.value,
      value = _props$value === void 0 ? '' : _props$value,
      propOnChange = props.onChange,
      onlySelectLeaf = props.onlySelectLeaf,
      restProps = _objectWithoutProperties(props, ["value", "onChange", "onlySelectLeaf"]);

  var ref = (0, _react.useRef)();

  var _onChange = function onChange(path, fileName) {
    propOnChange("".concat(path, "/").concat(fileName).replace(/\/\//g, '/'));
  };

  var fileArray = value.split('/');
  var name = fileArray.pop();
  var filePath = fileArray.join('/') || '/';
  var realValue = onlySelectLeaf ? value : filePath;

  var selectDom = /*#__PURE__*/_react.default.createElement(_antd.TreeSelect, _extends({
    showSearch: true,
    style: {
      width: onlySelectLeaf ? '100%' : '30%'
    },
    dropdownStyle: {
      backgroundColor: '#23232e'
    },
    labelInValue: true,
    dropdownMatchSelectWidth: onlySelectLeaf ? undefined : 400,
    value: {
      value: realValue
    } // 不加这个不会跟着进度条走
    ,
    getPopupContainer: function getPopupContainer() {
      return ref.current || document.body;
    },
    onSelect: function onSelect(_, node) {
      if (onlySelectLeaf) {
        propOnChange(node.value);
        return;
      }

      if (_onChange) {
        _onChange(node.value, name);
      }
    }
  }, restProps));

  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref,
    style: {
      position: 'relative'
    }
  }, onlySelectLeaf ? selectDom : /*#__PURE__*/_react.default.createElement(InputGroup, {
    compact: true
  }, selectDom, /*#__PURE__*/_react.default.createElement(_antd.Input, {
    style: {
      width: '70%'
    },
    value: name,
    onChange: function onChange(e) {
      _onChange(filePath, e.target.value);
    }
  })));
};

var _default = CustomTreeSelect;
exports.default = _default;