"use strict";

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

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

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

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

var _uuid = _interopRequireDefault(require("uuid"));

var _cache = _interopRequireDefault(require("./cache"));

var _getLocale = require("../../utils/getLocale");

var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6;

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var SearchWrapper = _styledComponents["default"].div(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n  width: 154px;\n  height: 22px;\n"])));

var InputWrapper = _styledComponents["default"].div(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n  position: relative;\n  z-index: 1;\n  padding-right: 40px;\n  padding-left: 5px;\n  overflow: hidden;\n  input {\n    border: 0px none;\n    min-height: 20px;\n    line-height: 20px;\n    margin-bottom: 2px;\n    padding: 0;\n    background-color: transparent;\n    width: 100%;\n    display: inline-block;\n  }\n  &::before {\n    content: '';\n    position: absolute;\n    bottom: 0;\n    height: 1px;\n    left: 0;\n    right: 0;\n    background-color: #dedede;\n  }\n  &::after {\n    content: '';\n    position: absolute;\n    bottom: 0;\n    height: 1px;\n    left: ", ";\n    right: ", ";\n    background-color: #1b85d6;\n    transition: all 0.3s ease;\n  }\n"])), function (p) {
  return p.active ? "0%" : "50%";
}, function (p) {
  return p.active ? "0%" : "50%";
});

var PlaceHolder = _styledComponents["default"].label(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n  position: absolute;\n  left: 0px;\n  top: 0px;\n  z-index: 2;\n  cursor: text;\n  color: #acb7bf;\n  padding-left: 5px;\n  display: block;\n"])));

var IconWrapper = _styledComponents["default"].div(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n  position: absolute;\n  width: 40px;\n  height: 20px;\n  top: 0;\n  right: 0;\n  display: flex;\n  align-items: center;\n  justify-content: flex-end;\n  transform: ", ";\n  transition: transform 0.3s ease;\n"])), function (p) {
  return p.active ? "translateX(0)" : "translateX(19px)";
});

var SearchIcon = _styledComponents["default"].i(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n  font-size: 14px;\n  cursor: pointer;\n  color: ", ";\n"])), function (p) {
  return p.active ? "#1b85d6" : "#D8D8D8";
});

var ClearIcon = _styledComponents["default"].i(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n  font-size: 14px;\n  color: #1b85d6;\n  margin-left: 5px;\n  transition: all 0.5s ease;\n  cursor: pointer;\n  opacity: ", ";\n  transform: ", ";\n"])), function (p) {
  return p.active ? 1 : 0;
}, function (p) {
  return p.active ? "scale(1)" : "scale(0)";
});

var Search = /*#__PURE__*/function (_PureComponent) {
  _inherits(Search, _PureComponent);

  var _super = _createSuper(Search);

  function Search(p) {
    var _this;

    _classCallCheck(this, Search);

    _this = _super.call(this, p);

    _this.handleEnterPress = function (e) {
      if (e.nativeEvent.keyCode == 13) {
        _this.handleSearch();
      }
    };

    _this.handleChange = function (e) {
      _this.setState({
        keywords: e.target.value
      });
    };

    _this.handleFocus = function () {
      if (_this.state.active) return;

      _this.setState({
        active: true
      });
    };

    _this.handleBlur = function () {
      var keywords = _this.state.keywords;

      if (!keywords || !keywords.length) {
        _this.setState({
          active: false
        });
      }
    };

    _this.handleSearch = function () {
      var keywords = _this.state.keywords;

      _this.props.onSearch(keywords);
    };

    _this.handleClear = function () {
      _this.setState({
        keywords: "",
        active: false
      });

      _this.props.onSearch("");
    };

    var cache = _cache["default"].getCache();

    _this.state = {
      id: (0, _uuid["default"])(),
      active: false,
      keywords: cache && cache.keywords || ""
    };
    return _this;
  }

  _createClass(Search, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          _this$props$placeHold = _this$props.placeHolder,
          placeHolder = _this$props$placeHold === void 0 ? (0, _getLocale.getLocale)("OKR_MyO_Pop_Pleaenterwords") : _this$props$placeHold,
          HTMLid = _this$props.id;
      var _this$state = this.state,
          id = _this$state.id,
          keywords = _this$state.keywords,
          active = _this$state.active;
      return /*#__PURE__*/_react["default"].createElement(SearchWrapper, {
        id: HTMLid
      }, /*#__PURE__*/_react["default"].createElement(InputWrapper, {
        className: "input-wrapper",
        active: active
      }, /*#__PURE__*/_react["default"].createElement(PlaceHolder, {
        htmlFor: id,
        style: {
          display: keywords && keywords.length ? "none" : "block"
        }
      }, placeHolder), /*#__PURE__*/_react["default"].createElement("input", {
        id: id,
        value: keywords,
        onChange: this.handleChange,
        onKeyPress: this.handleEnterPress,
        onFocus: this.handleFocus,
        onBlur: this.handleBlur
      }), /*#__PURE__*/_react["default"].createElement(IconWrapper, {
        active: active
      }, /*#__PURE__*/_react["default"].createElement(SearchIcon, {
        className: "tu-icon-search",
        active: active,
        onClick: this.handleSearch
      }), /*#__PURE__*/_react["default"].createElement(ClearIcon, {
        className: "tu-icon-close",
        active: active,
        onClick: this.handleClear
      }))));
    }
  }]);

  return Search;
}(_react.PureComponent);

exports["default"] = Search;