
(function (can, GGRC) {
  'use strict';

  GGRC.Components('mapperFilter', {
    tag: 'mapper-filter',
    template: can.view(
      GGRC.mustache_path +
      '/components/unified-mapper/mapper-filter.mustache'
    ),
    viewModel: {
      define: {
        filter: {
          type: 'string',
          set: function (newValue) {
            this.checkExpression(newValue);
            return newValue;
          }
        }
      },
      isExpression: false,
      checkExpression: function (filter) {
        var currentFilter = GGRC.query_parser.parse(filter);
        var isExpression =
          !!currentFilter && !!currentFilter.expression.op &&
          currentFilter.expression.op.name !== 'text_search' &&
          currentFilter.expression.op.name !== 'exclude_text_search';
        this.attr('isExpression', isExpression);
      },
      onSubmit: function () {
        this.dispatch('submit');
      },
      reset: function () {
        this.attr('filter', '');
      }
    },
    events: {
      '#mapper-filter input': function (el) {
        this.viewModel.attr('filter', el.val());
      }
    }
  });
})(window.can, window.GGRC);
