"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ColorPalettesSearch = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Icon = _interopRequireDefault(require("@material-ui/core/Icon"));
var _InputAdornment = _interopRequireDefault(require("@material-ui/core/InputAdornment"));
var _Search = _interopRequireDefault(require("@material-ui/icons/Search"));
var _Refresh = _interopRequireDefault(require("@material-ui/icons/Refresh"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));
var _i18n = require("../../../../../Framework/i18n");
var _IconButton = _interopRequireDefault(require("@material-ui/core/IconButton"));
var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));
var _styles = require("@material-ui/core/styles");
var _withWidth = require("@material-ui/core/withWidth");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var styles = function styles(theme) {
  return {
    textFieldInput: {
      padding: 10,
      marginTop: 1
    },
    noColorWrapper: {
      color: theme.palette.secondary.light,
      textAlign: 'center'
    },
    searchCloseTypography: {
      color: theme.palette.secondary.light,
      display: 'inherit',
      paddingTop: 15,
      '& span:last-child': {
        marginLeft: 10
      },
      '&:hover': {
        cursor: 'pointer'
      }
    },
    iconNoColorFound: {
      color: theme.palette.secondary.light,
      fontSize: 72
    },
    colorsWrap: function colorsWrap(_ref) {
      var heightColorPaletteWrapper = _ref.heightColorPaletteWrapper,
        width = _ref.width;
      return {
        display: 'flex',
        padding: '0 16px',
        maxHeight: (0, _withWidth.isWidthDown)('xs', width) ? heightColorPaletteWrapper : 250,
        flexWrap: 'wrap',
        overflowY: 'auto'
      };
    },
    colorSearchWrap: {
      display: 'flex',
      justifyContent: 'center'
    }
  };
};
var ColorPalettesSearch = exports.ColorPalettesSearch = (0, _styles.withStyles)(styles, {
  name: 'ColorPalettesSearch'
})(function (props) {
  var _useState = (0, _react.useState)(props.searchPalette),
    _useState2 = _slicedToArray(_useState, 2),
    filteredColors = _useState2[0],
    setFilteredColors = _useState2[1];
  var onSearch = function onSearch(searchText) {
    var searchPalette = props.searchPalette;
    var filterColors = searchPalette.colors && {
      colors: searchPalette.colors.filter(function (color) {
        return color.title.toLowerCase().indexOf(searchText.toLowerCase()) !== -1;
      })
    };
    var filteredColorPalette = _objectSpread(_objectSpread({}, searchPalette), {}, {
      colors: filterColors.colors
    });
    setFilteredColors(filteredColorPalette);
  };
  var keyUp = function keyUp() {
    onSearch(props.searchText);
  };
  return props.isSearchActive && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
    variant: "outlined",
    fullWidth: true,
    value: props.searchText,
    onChange: props.changeText,
    onKeyUp: keyUp,
    margin: "normal",
    translate: "no",
    InputProps: {
      endAdornment: /*#__PURE__*/_react["default"].createElement(_InputAdornment["default"], {
        position: "end"
      }, /*#__PURE__*/_react["default"].createElement(_IconButton["default"], {
        "aria-label": "Toggle password visibility",
        onClick: function onClick() {
          return onSearch(props.searchText);
        }
      }, /*#__PURE__*/_react["default"].createElement(_Search["default"], null))),
      classes: {
        inputAdornedEnd: props.classes.textFieldInput
      }
    }
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: props.classes.colorSearchWrap
  }, /*#__PURE__*/_react["default"].createElement(_Typography["default"], {
    classes: {
      root: props.classes.searchCloseTypography
    },
    onClick: props.closeSearch,
    noWrap: true
  }, /*#__PURE__*/_react["default"].createElement(_Refresh["default"], null), /*#__PURE__*/_react["default"].createElement("span", null, (0, _i18n.t)('colorPalettes.resetSearch')))), filteredColors.colors.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: props.classes.colorsWrap
  }, props.renderColors(filteredColors)), filteredColors && filteredColors.colors && filteredColors.colors.length === 0 && /*#__PURE__*/_react["default"].createElement(_Typography["default"], {
    component: "div",
    classes: {
      root: props.classes.noColorWrapper
    }
  }, /*#__PURE__*/_react["default"].createElement(_Icon["default"], {
    classes: {
      root: props.classes.iconNoColorFound
    }
  }, "format_color_reset"), /*#__PURE__*/_react["default"].createElement("div", null, (0, _i18n.t)('colorPalettes.colorNotFound'))));
});