"use strict";

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

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

var _reactMaterialize = require("react-materialize");

var _modularityFront = _interopRequireDefault(require("modularity-front"));

var _ExactFilter = _interopRequireDefault(require("./UserInputs/ExactFilter"));

var _SearchFilter = _interopRequireDefault(require("./UserInputs/SearchFilter"));

var _RangeFilter = _interopRequireDefault(require("./UserInputs/RangeFilter"));

var _DropdownFilter = _interopRequireDefault(require("./UserInputs/DropdownFilter"));

require("./FilterModule.css");

var _Divider = _interopRequireDefault(require("@material-ui/core/Divider"));

var _DictionaryService = _interopRequireDefault(require("../Services/DictionaryService"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { 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); }

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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

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

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 } }); 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); }

var Module = _modularityFront.default.Module;

var FilterModule =
/*#__PURE__*/
function (_Modularity$Module) {
  _inherits(FilterModule, _Modularity$Module);

  function FilterModule(props) {
    var _this;

    _classCallCheck(this, FilterModule);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(FilterModule).call(this, props));
    _this.state = {
      filters: []
    };
    return _this;
  }

  _createClass(FilterModule, [{
    key: "setFilters",
    value: function setFilters(filter) {
      var _this2 = this;

      var fils = this.Inputs.value || [];
      var flag = false;
      var indx;
      fils = fils.map(function (item, index) {
        if (item.name === filter.name) {
          indx = index;
          flag = true;
          return filter;
        }

        return item;
      });

      if (flag === false) {
        // should not happen
        fils.push(filter);
      }

      if (filter.values) {
        if (filter.values[0] === "None") {
          filter.values = [];
        }

        if (filter.type === "range") {
          if (filter.options[0] === filter.values[0] && filter.options[1] === filter.values[1]) {
            delete filter.values;
          }
        }
      }

      setTimeout(function () {
        return _this2.setState({
          filters: fils
        });
      }, 10);
      this.Filters.set(fils);
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var filters = this.Inputs.value.map(function (filter, index) {
        switch (filter.type) {
          case 'exact':
          case 'manytomany':
          case 'onetoone':
            return _react.default.createElement(_ExactFilter.default, {
              Filters: function Filters(filters) {
                return _this3.setFilters(filters);
              },
              filter: filter,
              key: index
            });

          case 'search':
            return _react.default.createElement(_SearchFilter.default, {
              Filters: function Filters(filters) {
                return filters ? _this3.setFilters(filters) : null;
              },
              filter: filter,
              key: index
            });

          case 'range':
            return _react.default.createElement(_RangeFilter.default, {
              Filters: function Filters(filters) {
                return filters ? _this3.setFilters(filters) : null;
              },
              filter: filter,
              key: index,
              refreshTime: 500
            });

          case 'select':
            return _react.default.createElement(_DropdownFilter.default, {
              Filters: function Filters(filters) {
                return filters ? _this3.setFilters(filters) : null;
              },
              filter: filter,
              key: index
            });
        }
      });
      return _react.default.createElement(_reactMaterialize.Row, {
        className: "FilterModule " + (this.props.className || "filter_layout"),
        style: this.props.style
      }, _react.default.createElement(_reactMaterialize.Col, {
        s: 12
      }, _react.default.createElement("div", {
        className: "filters-title"
      }, _DictionaryService.default.get("Filters")), _react.default.createElement(_Divider.default, null), _get(_getPrototypeOf(FilterModule.prototype), "render", this).call(this)), _react.default.createElement(_reactMaterialize.Col, {
        s: 12,
        className: "flex-container"
      }, filters.map(function (f, i) {
        return _react.default.createElement(_reactMaterialize.Row, {
          key: i
        }, _react.default.createElement(_reactMaterialize.Col, {
          s: 12
        }, f));
      })));
    }
  }]);

  return FilterModule;
}(_modularityFront.default.Module);

FilterModule.EditPosition = "center";
FilterModule.Inputs = {
  Inputs: {
    type: Module.Types.Array,
    defaultValue: [{
      type: "range",
      name: "Price Range",
      options: [0, 100],
      field: "price"
    }, {
      type: "search",
      name: "makrs2",
      options: "hp2",
      field: "name"
    }, {
      type: "exact",
      name: "Type1",
      options: ["hp", "canon", "samsung"],
      field: "name"
    }, {
      type: "select",
      name: "Type2",
      options: ["hp", "samsung", "canon"],
      field: "name"
    }],
    arrType: Module.Types.Object
  }
};
FilterModule.Outputs = {
  Filters: {}
};
FilterModule.Category = 'sxoinas';
var _default = FilterModule;
exports.default = _default;