import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ServerSideTypeModel } from './services/ServerSideFieldModel';
import { I18nService } from './services/system/locale/I18n';
import { RULE_CONDITION_GROUP_UPDATE_OPERATOR, RULE_CONDITION_CREATE, ConditionModel } from './services/Rule';
import { LoggerService } from 'dotcms-js/dotcms-js';
var ConditionGroupComponent = (function () {
    function ConditionGroupComponent(resources, loggerService) {
        this.loggerService = loggerService;
        this.groupIndex = 0;
        this.deleteConditionGroup = 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;
        this._rsrcCache = {};
    }
    ConditionGroupComponent.prototype.ngOnChanges = function (changes) {
        if (changes.group && this.group && this.group._conditions.length === 0) {
            this.group._conditions.push(new ConditionModel({ _type: new ServerSideTypeModel() }));
        }
    };
    ConditionGroupComponent.prototype.rsrc = function (subkey) {
        var x = this._rsrcCache[subkey];
        if (!x) {
            x = this.resources.get(ConditionGroupComponent.I8N_BASE + '.' + subkey);
            this._rsrcCache[subkey] = x;
        }
        return x;
    };
    ConditionGroupComponent.prototype.onCreateCondition = function () {
        this.loggerService.info('ConditionGroupComponent', 'onCreateCondition');
        this.createCondition.emit({
            payload: {
                conditionGroup: this.group,
                index: this.groupIndex,
                type: RULE_CONDITION_CREATE
            }
        });
    };
    ConditionGroupComponent.prototype.toggleGroupOperator = function () {
        // tslint:disable-next-line:prefer-const
        var value = this.group.operator === 'AND' ? 'OR' : 'AND';
        this.updateConditionGroupOperator.emit({
            payload: {
                conditionGroup: this.group,
                index: this.groupIndex,
                type: RULE_CONDITION_GROUP_UPDATE_OPERATOR,
                value: value
            }
        });
    };
    return ConditionGroupComponent;
}());
export { ConditionGroupComponent };
ConditionGroupComponent.I8N_BASE = 'api.sites.ruleengine.rules';
ConditionGroupComponent.decorators = [
    { type: Component, args: [{
                selector: 'condition-group',
                template: "<div class=\"cw-rule-group\">\n  <div class=\"cw-condition-group-separator\" *ngIf=\"groupIndex === 0\">\n    {{rsrc('inputs.group.whenConditions.label') | async}}\n  </div>\n  <div class=\"cw-condition-group-separator\" *ngIf=\"groupIndex !== 0\">\n    <div class=\"ui basic icon buttons\">\n      <button class=\"ui small button cw-group-operator\" (click)=\"toggleGroupOperator()\">\n        <div>{{group.operator}}</div>\n      </button>\n    </div>\n    <span flex class=\"cw-header-text\">\n    {{rsrc('inputs.group.whenFurtherConditions.label') | async}}</span>\n  </div>\n  <div flex layout=\"column\" class=\"cw-conditions\">\n    <div layout=\"row\"\n         class=\"cw-condition-row\"\n         *ngFor=\"let condition of group?._conditions; let i=index\">\n      <rule-condition flex layout=\"row\"\n                      [condition]=\"condition\"\n                      [conditionTypes]=\"conditionTypes\"\n                      [conditionTypePlaceholder]=\"conditionTypePlaceholder\"\n                      [index]=\"i\"\n                      (deleteCondition)=\"deleteCondition.emit($event)\"\n                      (updateConditionType)=\"updateConditionType.emit($event)\"\n                      (updateConditionParameter)=\"updateConditionParameter.emit($event)\"\n                      (updateConditionOperator)=\"updateConditionOperator.emit($event)\"\n                      ></rule-condition>\n      <div class=\"cw-btn-group cw-add-btn\">\n        <div class=\"ui basic icon buttons\" *ngIf=\"i === (group?._conditions.length - 1)\">\n          <button class=\"cw-button-add-item ui button\" arial-label=\"Add Condition\"\n            (click)=\"onCreateCondition()\" [disabled]=\"!condition.isPersisted()\">\n            <i class=\"plus icon\" aria-hidden=\"true\"></i>\n          </button>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>\n\n"
            },] },
];
/** @nocollapse */
ConditionGroupComponent.ctorParameters = function () { return [
    { type: I18nService, },
    { type: LoggerService, },
]; };
ConditionGroupComponent.propDecorators = {
    'group': [{ type: Input },],
    'conditionTypePlaceholder': [{ type: Input },],
    'groupIndex': [{ type: Input },],
    'conditionTypes': [{ type: Input },],
    'deleteConditionGroup': [{ type: Output },],
    'updateConditionGroupOperator': [{ type: Output },],
    'createCondition': [{ type: Output },],
    'deleteCondition': [{ type: Output },],
    'updateConditionType': [{ type: Output },],
    'updateConditionParameter': [{ type: Output },],
    'updateConditionOperator': [{ type: Output },],
};
//# sourceMappingURL=rule-condition-group-component.js.map