import { Component, EventEmitter, Input, Output } from '@angular/core';
import { I18nService } from './services/system/locale/I18n';
import { RULE_CONDITION_UPDATE_PARAMETER, RULE_CONDITION_UPDATE_TYPE, RULE_CONDITION_DELETE, RULE_CONDITION_UPDATE_OPERATOR } from './services/Rule';
import { LoggerService } from 'dotcms-js/dotcms-js';
var ConditionComponent = (function () {
    function ConditionComponent(_resources, loggerService) {
        this._resources = _resources;
        this.loggerService = loggerService;
        this.conditionTypes = {};
        this.conditionTypePlaceholder = '';
        this.updateConditionType = new EventEmitter(false);
        this.updateConditionParameter = new EventEmitter(false);
        this.updateConditionOperator = new EventEmitter(false);
        this.deleteCondition = new EventEmitter(false);
    }
    ConditionComponent.prototype.ngOnChanges = function (change) {
        var _this = this;
        try {
            if (change.condition) {
                if (this.typeDropdown && this.condition.type) {
                    if (this.condition.type.key !== 'NoSelection') {
                        this.typeDropdown.value = this.condition.type.key;
                    }
                }
            }
            if (change.conditionTypes && !this.typeDropdown) {
                this.typeDropdown = {
                    options: [],
                    placeholder: this._resources.get('api.sites.ruleengine.rules.inputs.condition.type.placeholder')
                };
                Object.keys(this.conditionTypes).forEach(function (key) {
                    var type = _this.conditionTypes[key];
                    _this.typeDropdown.options.push(type._opt);
                });
            }
        }
        catch (e) {
            this.loggerService.error('ConditionComponent', 'ngOnChanges', e);
        }
    };
    ConditionComponent.prototype.onTypeChange = function (type) {
        this.loggerService.info('ConditionComponent', 'onTypeChange', type);
        this.updateConditionType.emit({
            payload: { condition: this.condition, value: type, index: this.index },
            type: RULE_CONDITION_UPDATE_TYPE
        });
    };
    ConditionComponent.prototype.onParameterValuesChange = function (event) {
        var _this = this;
        event.forEach(function (change) { return _this.onParameterValueChange(change); });
    };
    ConditionComponent.prototype.onParameterValueChange = function (event) {
        this.loggerService.info('ConditionComponent', 'onParameterValueChange');
        this.updateConditionParameter.emit({
            payload: {
                condition: this.condition,
                name: event.name,
                value: event.value,
                index: this.index
            },
            type: RULE_CONDITION_UPDATE_PARAMETER
        });
    };
    ConditionComponent.prototype.toggleOperator = function () {
        var op = this.condition.operator === 'AND' ? 'OR' : 'AND';
        this.updateConditionOperator.emit({
            type: RULE_CONDITION_UPDATE_OPERATOR,
            payload: { condition: this.condition, value: op, index: this.index }
        });
    };
    ConditionComponent.prototype.onDeleteConditionClicked = function () {
        this.deleteCondition.emit({
            type: RULE_CONDITION_DELETE,
            payload: { condition: this.condition }
        });
    };
    return ConditionComponent;
}());
export { ConditionComponent };
ConditionComponent.decorators = [
    { type: Component, args: [{
                selector: 'rule-condition',
                template: "<div *ngIf=\"typeDropdown != null\" flex layout=\"row\" class=\"cw-condition cw-entry\">\n  <div class=\"cw-btn-group cw-condition-toggle\">\n    <button class=\"ui basic button cw-button-toggle-operator\" aria-label=\"Swap And/Or\" (click)=\"toggleOperator()\" *ngIf=\"index !== 0\">\n      {{condition.operator}}\n    </button>\n  </div>\n  <cw-input-dropdown\n      flex=\"25\"\n      class=\"cw-type-dropdown\"\n      [value]=\"condition.type?.key\"\n      placeholder=\"{{conditionTypePlaceholder}}\"\n      (onDropDownChange)=\"onTypeChange($event)\">\n    <cw-input-option\n        *ngFor=\"let opt of typeDropdown.options\"\n        [value]=\"opt.value\"\n        [label]=\"opt.label\"\n        icon=\"{{opt.icon}}\"></cw-input-option>\n  </cw-input-dropdown>\n  <div flex=\"75\" class=\"cw-condition-row-main\" [ngSwitch]=\"condition.type?.key\">\n    <ng-template [ngSwitchCase]=\"'NoSelection'\">\n      <div class=\"cw-condition-component\"></div>\n    </ng-template>\n    <ng-template [ngSwitchCase]=\"'VisitorsGeolocationConditionlet'\">\n      <cw-visitors-location-container\n          [componentInstance]=\"condition\"\n          (parameterValuesChange)=\"onParameterValuesChange($event)\"></cw-visitors-location-container>\n    </ng-template>\n    <ng-template ngSwitchDefault>\n      <cw-serverside-condition class=\"cw-condition-component\"\n                               [componentInstance]=\"condition\"\n                               (parameterValueChange)=\"onParameterValueChange($event)\">\n      </cw-serverside-condition>\n    </ng-template>\n  </div>\n</div>\n<div class=\"cw-btn-group cw-delete-btn\">\n  <div class=\"ui basic icon buttons\">\n    <button class=\"ui button\" aria-label=\"Delete Condition\" (click)=\"onDeleteConditionClicked()\" [disabled]=\"!condition.isPersisted()\">\n      <i class=\"trash icon\"></i>\n    </button>\n  </div>\n</div>\n"
            },] },
];
/** @nocollapse */
ConditionComponent.ctorParameters = function () { return [
    { type: I18nService, },
    { type: LoggerService, },
]; };
ConditionComponent.propDecorators = {
    'condition': [{ type: Input },],
    'index': [{ type: Input },],
    'conditionTypes': [{ type: Input },],
    'conditionTypePlaceholder': [{ type: Input },],
    'updateConditionType': [{ type: Output },],
    'updateConditionParameter': [{ type: Output },],
    'updateConditionOperator': [{ type: Output },],
    'deleteCondition': [{ type: Output },],
};
//# sourceMappingURL=rule-condition-component.js.map