"use strict";

exports.__esModule = true;
exports.useStyles = exports.getSearchOptionValue = exports.default = 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(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
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 = exports.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: 1,
      border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.grey.borders),
      boxShadow: "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
        }
      },
      "&:focus-within": {
        zIndex: 99,
        border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.focus),
        boxShadow: "0 0 4px ".concat(theme.palette.focus)
      }
    },
    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"
      }
    }
  };
});
var getSearchOptionValue = exports.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;
};
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,
    _ref$focusAndSelectSe = _ref.focusAndSelectSearchFieldOnLoad,
    focusAndSelectSearchFieldOnLoad = _ref$focusAndSelectSe === void 0 ? true : _ref$focusAndSelectSe,
    _ref$focusSearchOnSea = _ref.focusSearchOnSearchOptionChange,
    focusSearchOnSearchOptionChange = _ref$focusSearchOnSea === void 0 ? false : _ref$focusSearchOnSea,
    _ref$trimSearchvalue = _ref.trimSearchvalue,
    trimSearchvalue = _ref$trimSearchvalue === void 0 ? true : _ref$trimSearchvalue;
  searchOptions = !((_searchOptions = searchOptions) != null && _searchOptions.length) ? null : searchOptions;
  searchOption = getSearchOptionValue(searchOptions, searchOption);
  var classes = useStyles();
  var inputRef = (0, _react.useRef)();
  var onSearchInternal = function onSearchInternal(newSearchOption, value) {
    var searchValue = trimSearchvalue ? value == null ? void 0 : value.trim() : value;
    onSearch(newSearchOption, searchValue);
  };
  var update = function update(newSearchOption) {
    if (focusSearchOnSearchOptionChange && inputRef.current) {
      onSearchInternal(newSearchOption, "");
      setTimeout(function () {
        /* istanbul ignore next */
        if (inputRef.current) {
          inputRef.current.value = "";
          inputRef.current.focus();
          inputRef.current.select();
        }
      }, 0);
    } else {
      var _inputRef$current;
      onSearchInternal(newSearchOption, (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.value);
    }
  };
  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);
  (0, _react.useEffect)(function () {
    /* istanbul ignore next */
    if (focusAndSelectSearchFieldOnLoad && inputRef.current) {
      inputRef.current.focus();
      inputRef.current.select();
    }
  }, [focusAndSelectSearchFieldOnLoad]);
  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 onSubmit = function onSubmit(event) {
    var _inputRef$current2;
    // using form submit instead of a keydown (with key=enter) to allow the 'enter key' event to be canceled elsewhere to avoid the submit event
    onSearchInternal(searchOption, (_inputRef$current2 = inputRef.current) == null ? void 0 : _inputRef$current2.value);
    event.preventDefault();
  };
  var inputSection = /*#__PURE__*/_react.default.createElement("div", {
    "data-qa": "searchInput",
    className: classes.parentInput
  }, /*#__PURE__*/_react.default.createElement("form", {
    "data-qa": "searchForm",
    className: classes.fullWidth,
    onSubmitCapture: onSubmit
  }, /*#__PURE__*/_react.default.createElement(_Input.default, {
    placeholder: placeholder,
    defaultValue: defaultValue,
    inputRef: inputRef,
    type: "text",
    disabled: disabled,
    classes: {
      input: classes.controlInput
    },
    disableUnderline: true,
    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;
        onSearchInternal(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",
    "data-testid": "searchButton",
    variant: "contained",
    disabled: disabled,
    classes: {
      root: classes.searchButton
    },
    onClick: function onClick() {
      onSearchInternal(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, "useStyles{classes}\nuseRef{inputRef}\nuseEffect{}", function () {
  return [useStyles];
});
__signature__(SearchControl, "useStyles{classes}\nuseRef{inputRef}\nuseEffect{}", function () {
  return [useStyles];
});
var _default = SearchControl;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(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);
})();