import { Component, EventEmitter, Input, Output } from '@angular/core';
import { RULE_CREATE } from './services/Rule';
import { I18nService } from './services/system/locale/I18n';
import { CwFilter } from './services/util/CwFilter';
var I8N_BASE = 'api.sites.ruleengine';
/**
 *
 */
var RuleEngineComponent = (function () {
    function RuleEngineComponent(resources) {
        this.ruleActionTypes = {};
        this.conditionTypes = {};
        this.createRule = new EventEmitter(false);
        this.deleteRule = new EventEmitter(false);
        this.updateName = new EventEmitter(false);
        this.updateExpandedState = new EventEmitter(false);
        this.updateEnabledState = new EventEmitter(false);
        this.updateFireOn = new EventEmitter(false);
        this.createRuleAction = new EventEmitter(false);
        this.deleteRuleAction = new EventEmitter(false);
        this.updateRuleActionType = new EventEmitter(false);
        this.updateRuleActionParameter = new EventEmitter(false);
        this.createConditionGroup = new EventEmitter(false);
        this.updateConditionGroupOperator = new EventEmitter(false);
        this.createCondition = new EventEmitter(false);
        this.deleteCondition = new EventEmitter(false);
        this.updateConditionType = new EventEmitter(false);
        this.updateConditionParameter = new EventEmitter(false);
        this.updateConditionOperator = new EventEmitter(false);
        this.resources = resources;
        resources.get(I8N_BASE).subscribe(function (rsrc) { });
        this.filterText = '';
        this.rules = [];
        this._rsrcCache = {};
        this.status = null;
    }
    RuleEngineComponent.prototype.rsrc = function (subkey) {
        var x = this._rsrcCache[subkey];
        if (!x) {
            x = this.resources.get(I8N_BASE + '.rules.' + subkey);
            this._rsrcCache[subkey] = x;
        }
        return x;
    };
    RuleEngineComponent.prototype.ngOnChange = function (change) {
        if (change.rules) {
            this.updateActiveRuleCount();
        }
    };
    RuleEngineComponent.prototype.addRule = function () {
        this.createRule.emit({ type: RULE_CREATE });
    };
    RuleEngineComponent.prototype.updateActiveRuleCount = function () {
        this.activeRules = 0;
        for (var i = 0; i < this.rules.length; i++) {
            if (this.rules[i].enabled) {
                this.activeRules++;
            }
        }
    };
    RuleEngineComponent.prototype.setFieldFilter = function (field, value) {
        if (value === void 0) { value = null; }
        // remove old status
        var re = new RegExp(field + ':[\\w]*');
        this.filterText = this.filterText.replace(re, ''); // whitespace issues: "blah:foo enabled:false mahRule"
        if (value !== null) {
            this.filterText = field + ':' + value + ' ' + this.filterText;
        }
    };
    RuleEngineComponent.prototype.isFilteringField = function (field, value) {
        if (value === void 0) { value = null; }
        var isFiltering;
        if (value === null) {
            var re = new RegExp(field + ':[\\w]*');
            isFiltering = this.filterText.match(re) != null;
        }
        else {
            isFiltering = this.filterText.indexOf(field + ':' + value) >= 0;
        }
        return isFiltering;
    };
    RuleEngineComponent.prototype.isFiltered = function (rule) {
        return CwFilter.isFiltered(rule, this.filterText);
    };
    return RuleEngineComponent;
}());
export { RuleEngineComponent };
RuleEngineComponent.decorators = [
    { type: Component, args: [{
                selector: 'cw-rule-engine',
                template: "\n  <div class=\"cw-modal-glasspane\"  [class.cw-loading]=\"loading\" *ngIf=\"loading\"></div>\n  <div *ngIf=\"!loading && globalError\" class=\"ui negative message cw-message\">\n    <div class=\"header\">{{globalError}}</div>\n    <p>Please contact an administrator</p>\n  </div>\n<div class=\"cw-rule-engine\" *ngIf=\"!loading && showRules\">\n  <div class=\"cw-header\">\n    <div flex layout=\"row\" layout-align=\"space-between center\">\n      <div flex layout=\"row\" layout-align=\"space-between center\" class=\"ui icon input\">\n        <i class=\"filter icon\"></i>\n        <input class=\"cw-rule-filter\" type=\"text\" placeholder=\"{{rsrc('inputs.filter.placeholder') | async}}\"\n          [value]=\"filterText\" (keyup)=\"filterText = $event.target.value\">\n      </div>\n      <div flex=\"2\"></div>\n      <button class=\"ui button cw-button-add\" aria-label=\"Create a new rule\" (click)=\"addRule()\">\n        <i class=\"plus icon\" aria-hidden=\"true\"></i>{{rsrc('inputs.addRule.label') | async}}\n      </button>\n    </div>\n    <div class=\"cw-filter-links\">\n      <span>{{rsrc('inputs.filter.status.show.label') | async}}:</span>\n      <a href=\"javascript:void(0)\" class=\"cw-filter-link\" [class.active]=\"!isFilteringField('enabled')\"\n        (click)=\"setFieldFilter('enabled',null)\">{{rsrc('inputs.filter.status.all.label') | async}}</a>\n      <span>&#124;</span>\n      <a href=\"javascript:void(0)\" class=\"cw-filter-link\" [class.active]=\"isFilteringField('enabled',true)\"\n        (click)=\"setFieldFilter('enabled',true)\">{{rsrc('inputs.filter.status.active.label') | async}}</a>\n      <span>&#124;</span>\n      <a href=\"javascript:void(0)\" class=\"cw-filter-link\" [class.active]=\"isFilteringField('enabled',false)\"\n        (click)=\"setFieldFilter('enabled',false)\">{{rsrc('inputs.filter.status.inactive.label') | async}}</a>\n    </div>\n  </div>\n  <rule *ngFor=\"let rule of rules\" [rule]=\"rule\" [hidden]=\"isFiltered(rule) == true\"\n        [environmentStores]=\"environmentStores\"\n        [ruleActions]=\"rule._ruleActions\"\n        [ruleActionTypes]=\"ruleActionTypes\"\n        [conditionTypes]=\"conditionTypes\"\n        [saved]=\"rule._saved\"\n        [saving]=\"rule._saving\"\n        [errors]=\"rule._errors\"\n        (updateName)=\"updateName.emit($event)\"\n        (updateFireOn)=\"updateFireOn.emit($event)\"\n        (updateEnabledState)=\"updateEnabledState.emit($event)\"\n        (updateExpandedState)=\"updateExpandedState.emit($event)\"\n\n        (createRuleAction)=\"createRuleAction.emit($event)\"\n        (updateRuleActionType)=\"updateRuleActionType.emit($event)\"\n        (updateRuleActionParameter)=\"updateRuleActionParameter.emit($event)\"\n        (deleteRuleAction)=\"deleteRuleAction.emit($event)\"\n\n        (createCondition)=\"createCondition.emit($event)\"\n        (createConditionGroup)=\"createConditionGroup.emit($event)\"\n        (updateConditionGroupOperator)=\"updateConditionGroupOperator.emit($event)\"\n        (updateConditionType)=\"updateConditionType.emit($event)\"\n        (updateConditionParameter)=\"updateConditionParameter.emit($event)\"\n        (updateConditionOperator)=\"updateConditionOperator.emit($event)\"\n        (deleteCondition)=\"deleteCondition.emit($event)\"\n\n        (deleteRule)=\"deleteRule.emit($event)\"></rule>\n</div>\n\n"
            },] },
];
/** @nocollapse */
RuleEngineComponent.ctorParameters = function () { return [
    { type: I18nService, },
]; };
RuleEngineComponent.propDecorators = {
    'rules': [{ type: Input },],
    'ruleActionTypes': [{ type: Input },],
    'loading': [{ type: Input },],
    'globalError': [{ type: Input },],
    'showRules': [{ type: Input },],
    'conditionTypes': [{ type: Input },],
    'environmentStores': [{ type: Input },],
    'createRule': [{ type: Output },],
    'deleteRule': [{ type: Output },],
    'updateName': [{ type: Output },],
    'updateExpandedState': [{ type: Output },],
    'updateEnabledState': [{ type: Output },],
    'updateFireOn': [{ type: Output },],
    'createRuleAction': [{ type: Output },],
    'deleteRuleAction': [{ type: Output },],
    'updateRuleActionType': [{ type: Output },],
    'updateRuleActionParameter': [{ type: Output },],
    'createConditionGroup': [{ type: Output },],
    'updateConditionGroupOperator': [{ type: Output },],
    'createCondition': [{ type: Output },],
    'deleteCondition': [{ type: Output },],
    'updateConditionType': [{ type: Output },],
    'updateConditionParameter': [{ type: Output },],
    'updateConditionOperator': [{ type: Output },],
};
//# sourceMappingURL=rule-engine.js.map