"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.SearchInputField = exports.getInputHeight = void 0;

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _randomstring = _interopRequireDefault(require("randomstring"));

var _typography = require("../../typography");

var _hideAccessibly = require("../../../primitives/hide-accessibly");

var _branding = require("../../../mixins/branding");

var _typography2 = require("../../../mixins/typography");

var _icons = require("./icons");

var _searchInputField = require("./search-input-field.css-rules");

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

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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 getInputHeight = function getInputHeight(_ref) {
  var large = _ref.large;
  return large ? '60px' : '40px';
};

exports.getInputHeight = getInputHeight;

var StyledSearchInputField = _styledComponents["default"].input.attrs(function (_ref2) {
  var bold = _ref2.bold,
      large = _ref2.large;
  return {
    fontSize: large ? 'f3' : 'f1',
    bold: large ? 'true' : bold
  };
}).withConfig({
  displayName: "StyledSearchInputField",
  componentId: "sc-1vzxzo7-0"
})(["&&{background-color:", ";background-repeat:no-repeat;border:1px solid ", ";border-radius:6px;color:", ";font-family:", ";font-size:", ";font-weight:", ";height:", ";padding:0;padding-left:", ";padding-right:40px;width:100%;&&::placeholder{color:", ";opacity:1;}&:focus{border:1px solid ", ";outline:none;padding-left:", ";&&::placeholder{color:transparent;opacity:0;}}}"], (0, _branding.getPaletteColor)('white'), (0, _branding.getPaletteColor)('greyscale-light'), (0, _branding.getPaletteColor)('greyscale-black'), _typography2.getFontFamily, _typography2.getFontSize, _typography2.getFontWeight, getInputHeight, _searchInputField.getSearchInputLeftPadding, (0, _branding.getPaletteColor)('greyscale-medium'), (0, _branding.getPaletteColor)('greyscale-medium'), _searchInputField.getSearchInputFocusedLeftPadding);

var SearchInputIcon = function SearchInputIcon(_ref3) {
  var focused = _ref3.focused,
      hasValue = _ref3.hasValue,
      large = _ref3.large;
  var showGreySearchIcon = !focused && !hasValue;
  var showFocusedSearchIcon = focused && !hasValue;

  if (showGreySearchIcon) {
    return _react["default"].createElement(_icons.LeftStyledSearchIcon, {
      large: large
    });
  }

  if (showFocusedSearchIcon) {
    return _react["default"].createElement(_icons.RightStyledSearchIcon, {
      large: large
    });
  }

  return null;
};

var SearchInputField = function SearchInputField(_ref4) {
  var value = _ref4.value,
      _ref4$large = _ref4.large,
      large = _ref4$large === void 0 ? false : _ref4$large,
      _ref4$bold = _ref4.bold,
      bold = _ref4$bold === void 0 ? false : _ref4$bold,
      _ref4$hideLabel = _ref4.hideLabel,
      hideLabel = _ref4$hideLabel === void 0 ? false : _ref4$hideLabel,
      label = _ref4.label,
      placeholder = _ref4.placeholder,
      onChange = _ref4.onChange,
      focusOnMount = _ref4.focusOnMount;

  // Focused state
  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      focused = _useState2[0],
      setFocused = _useState2[1];

  var handleSearchInputFocused = (0, _react.useCallback)(function () {
    setFocused(true);
  }, [setFocused]);
  var handleSearchInputBlurred = (0, _react.useCallback)(function () {
    setFocused(false);
  }, [setFocused]);
  var handleOnChange = (0, _react.useCallback)(function (evt) {
    onChange(evt.target.value, evt);
  }, [onChange]);
  var handleClear = (0, _react.useCallback)(function () {
    onChange('');
  }, [onChange]);
  var inputId = "input-".concat(_randomstring["default"].generate(5));
  var inputProps = {
    id: inputId,
    large: large,
    bold: bold ? 'true' : undefined,
    type: 'text',
    value: value,
    autoFocus: focusOnMount,
    onChange: handleOnChange,
    onBlur: handleSearchInputBlurred,
    onFocus: handleSearchInputFocused,
    placeholder: placeholder,
    hasDarkIcon: !!value
  };
  return _react["default"].createElement("div", {
    style: {
      position: 'relative'
    }
  }, hideLabel ? _react["default"].createElement(_hideAccessibly.HideAccessibly, null, _react["default"].createElement(_typography.FieldLabel, {
    htmlFor: inputId
  }, label)) : _react["default"].createElement(_typography.FieldLabel, {
    htmlFor: inputId,
    large: large
  }, label), _react["default"].createElement("div", {
    style: {
      position: 'relative'
    }
  }, _react["default"].createElement(SearchInputIcon, {
    focused: focused,
    hasValue: !!value.length,
    large: large
  }), _react["default"].createElement(StyledSearchInputField, inputProps), _react["default"].createElement(_icons.ClearInputButton, {
    large: large,
    onClick: handleClear,
    show: !!value.length
  })));
};

exports.SearchInputField = SearchInputField;
SearchInputField.displayName = 'SearchInputField';