"use strict";

exports.__esModule = true;
exports.default = exports.getSearchOptionValue = exports.useStyles = void 0;

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

var _styles = require("@material-ui/core/styles");

var _SelectProps = _interopRequireDefault(require("../SelectProps"));

var _Icon = _interopRequireDefault(require("../../DataDisplay/Icon"));

var _IconButton = _interopRequireDefault(require("@material-ui/core/IconButton"));

var _InputAdornment = _interopRequireDefault(require("@material-ui/core/InputAdornment"));

var _Select = _interopRequireDefault(require("../Select"));

var _Input = _interopRequireDefault(require("@material-ui/core/Input"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    container: {
      display: "flex",
      marginRight: theme.spacing(1),
      width: theme.spacing(48),
      "& > .MuiInputBase-root.MuiInput-root": {
        flex: "7 0 0"
      }
    },
    fullWidth: {
      width: "100%"
    },
    clearButton: {
      fontSize: theme.spacing(2.2),
      padding: theme.spacing(0.3),
      marginRight: theme.spacing(-0.1),
      borderRadius: 0,
      border: "none",
      "&.MuiIconButton-root": {
        marginLeft: "0"
      },
      "&:active": {
        boxShadow: "none",
        borderRadius: 0
      },
      "&:focus": {
        boxShadow: "none",
        borderRadius: 0
      }
    },
    searchButton: {
      flex: "1 0 0",
      fontSize: theme.spacing(2.2),
      padding: "".concat(theme.spacing(0.3), " ").concat(theme.spacing(1)),
      borderTopLeftRadius: 0,
      borderBottomLeftRadius: 0,
      zIndex: 10,
      "&.MuiIconButton-root": {
        marginLeft: "0"
      },
      "&:active": {
        borderTopLeftRadius: 0,
        borderBottomLeftRadius: 0
      },
      "&:focus": {
        borderTopLeftRadius: 0,
        borderBottomLeftRadius: 0
      }
    },
    controlInput: {
      borderRadius: 0,
      height: theme.spacing(1.6),
      width: "100%",
      border: "none",
      "&:active": {
        borderRadius: 0
      },
      "&:focus": {
        borderRadius: 0,
        boxShadow: "none"
      },
      "&::-ms-clear": {
        display: "none",
        height: 0,
        width: 0
      },
      "&:invalid ~ .MuiInputAdornment-root": {
        display: "none"
      },
      "&:valid ~ .MuiInputAdornment-root": {}
    },
    parentInput: {
      flex: "13 0 0",
      zIndex: function zIndex(props) {
        return props.focused ? 99 : 1;
      },
      border: function border(props) {
        return props.focused ? "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.focus) : "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.grey.borders);
      },
      boxShadow: function boxShadow(props) {
        return props.focused ? "0 0 4px ".concat(theme.palette.focus) : "none";
      },
      width: "100%",
      display: "inherit",
      marginLeft: theme.spacing(-0.1),
      marginRight: theme.spacing(-0.1),
      borderTopLeftRadius: theme.shape.borderRadius,
      borderBottomLeftRadius: theme.shape.borderRadius,
      "& $controlInput": {
        borderTopLeftRadius: theme.shape.borderRadius,
        borderBottomLeftRadius: theme.shape.borderRadius
      },
      ".MuiInputBase-root ~ &": {
        borderTopLeftRadius: 0,
        borderBottomLeftRadius: 0,
        "& $controlInput": {
          borderTopLeftRadius: 0,
          borderBottomLeftRadius: 0
        }
      }
    },
    selectRoot: {
      zIndex: 10,
      minWidth: "auto",
      maxWidth: "none",
      backgroundColor: theme.palette.grey.light,
      height: theme.spacing(3),
      boxSizing: "border-box",
      borderTopRightRadius: 0,
      borderBottomRightRadius: 0,
      "&:active": {
        borderTopRightRadius: 0,
        borderBottomRightRadius: 0
      },
      "&:focus": {
        backgroundColor: theme.palette.grey.light
      }
    },
    selectPaper: {
      "& ul": {
        maxWidth: "none"
      }
    }
  };
});
exports.useStyles = useStyles;

var getSearchOptionValue = function getSearchOptionValue(searchOptions, searchOption) {
  if ((searchOptions == null ? void 0 : searchOptions.length) > 0) {
    var option = searchOptions.find(function (o) {
      return o.value === searchOption;
    });

    if (option) {
      return option.value;
    }

    return searchOptions[0].value;
  }

  return null;
};

exports.getSearchOptionValue = getSearchOptionValue;

var SearchControl = function SearchControl(_ref) {
  var _searchOptions;

  var placeholder = _ref.placeholder,
      _ref$defaultValue = _ref.defaultValue,
      defaultValue = _ref$defaultValue === void 0 ? "" : _ref$defaultValue,
      searchOptions = _ref.searchOptions,
      searchOption = _ref.searchOption,
      _ref$onSearch = _ref.onSearch,
      onSearch = _ref$onSearch === void 0 ? function () {} : _ref$onSearch,
      disabled = _ref.disabled;
  searchOptions = !((_searchOptions = searchOptions) != null && _searchOptions.length) ? null : searchOptions;
  searchOption = getSearchOptionValue(searchOptions, searchOption);

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      inputFocused = _useState2[0],
      setInputFocused = _useState2[1];

  var classes = useStyles({
    focused: inputFocused
  });
  var inputRef = (0, _react.useRef)();

  var update = function update(value) {
    onSearch(value, defaultValue);
  };

  var selectProps = new _SelectProps.default();
  selectProps.set(_SelectProps.default.propNames.update, update);
  selectProps.set(_SelectProps.default.propNames.value, searchOption);
  selectProps.set(_SelectProps.default.propNames.positionOverride, {
    anchorOrigin: {
      vertical: "bottom",
      horizontal: "left"
    },
    transformOrigin: {
      vertical: "top",
      horizontal: "left"
    }
  });
  selectProps.setStyle(_SelectProps.default.ruleNames.root, classes.selectRoot);
  selectProps.setStyle(_SelectProps.default.ruleNames.paper, classes.selectPaper);

  var handleKeyDown = function handleKeyDown(e) {
    if (e.key === "Enter") {
      onSearch(searchOption, e.target.value);
      e.preventDefault();
      e.stopPropagation();
    }
  };

  var onFocusedEvent = function onFocusedEvent(event, focused) {
    setInputFocused(focused);
    event.preventDefault();
    event.stopPropagation();
  };

  var SelectSection = function SelectSection() {
    if (searchOptions === null) return null;else return /*#__PURE__*/_react.default.createElement(_Select.default, {
      className: classes.selectInput,
      options: searchOptions,
      selectProps: selectProps
    });
  };

  var inputSection = /*#__PURE__*/_react.default.createElement("div", {
    "data-qa": "searchInput",
    "data-qa-is-focused": inputFocused,
    className: classes.parentInput
  }, /*#__PURE__*/_react.default.createElement("form", {
    "data-qa": "searchForm",
    className: classes.fullWidth
  }, /*#__PURE__*/_react.default.createElement(_Input.default, {
    placeholder: placeholder,
    defaultValue: defaultValue,
    inputRef: inputRef,
    type: "text",
    disabled: disabled,
    classes: {
      input: classes.controlInput
    },
    onKeyDown: handleKeyDown,
    disableUnderline: true,
    onFocus: function onFocus(e) {
      return onFocusedEvent(e, true);
    },
    onBlur: function onBlur(e) {
      return onFocusedEvent(e, false);
    },
    endAdornment: /*#__PURE__*/_react.default.createElement(_InputAdornment.default, {
      position: "start"
    }, /*#__PURE__*/_react.default.createElement(_IconButton.default, {
      tabIndex: "-1",
      "data-qa": "clearButton",
      disabled: disabled,
      onClick: function onClick() {
        inputRef.current.value = null;
        onSearch(searchOption);
        inputRef.current.focus();
      },
      className: classes.clearButton
    }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
      id: "close2"
    })))
  })));

  var searchSection = /*#__PURE__*/_react.default.createElement(_IconButton.default, {
    "data-qa": "searchButton",
    variant: "contained",
    disabled: disabled,
    classes: {
      root: classes.searchButton
    },
    onClick: function onClick() {
      onSearch(searchOption, inputRef.current.value);
    }
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, {
    id: "search"
  }));

  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.container
  }, /*#__PURE__*/_react.default.createElement(SelectSection, null), inputSection, searchSection);
};

__signature__(SearchControl, "useState{[inputFocused, setInputFocused](false)}\nuseStyles{classes}\nuseRef{inputRef}", function () {
  return [useStyles];
});

__signature__(SearchControl, "useState{[inputFocused, setInputFocused](false)}\nuseStyles{classes}\nuseRef{inputRef}", function () {
  return [useStyles];
});

var _default = SearchControl;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(getSearchOptionValue, "getSearchOptionValue", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(SearchControl, "SearchControl", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();

;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(getSearchOptionValue, "getSearchOptionValue", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(SearchControl, "SearchControl", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/PredefinedElements/SearchControl.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();