/**
 *
 * @ai-apps/angular v2.147.1-next.0 | rule.component.js
 *
 * Copyright 2014, 2022 IBM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import { Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { I18n } from 'carbon-components-angular';
import * as i0 from "@angular/core";
import * as i1 from "carbon-components-angular";
import * as i2 from "@angular/common";
import * as i3 from "./rule-builder-group-logic.component";
import * as i4 from "@angular/forms";
function RuleComponent_ng_container_0_ng_container_2_Template(rf, ctx) { if (rf & 1) {
    const _r6 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "ai-rule", 3);
    i0.ɵɵlistener("addRule", function RuleComponent_ng_container_0_ng_container_2_Template_ai_rule_addRule_1_listener($event) { i0.ɵɵrestoreView(_r6); const ctx_r5 = i0.ɵɵnextContext(2); return ctx_r5.addRule.emit($event); })("removeRule", function RuleComponent_ng_container_0_ng_container_2_Template_ai_rule_removeRule_1_listener($event) { i0.ɵɵrestoreView(_r6); const ctx_r7 = i0.ɵɵnextContext(2); return ctx_r7.removeRule.emit($event); })("ruleChange", function RuleComponent_ng_container_0_ng_container_2_Template_ai_rule_ruleChange_1_listener($event) { i0.ɵɵrestoreView(_r6); const i_r4 = ctx.index; const ctx_r8 = i0.ɵɵnextContext(2); return (ctx_r8.rule.rules[i_r4] = $event); });
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const i_r4 = ctx.index;
    const ctx_r2 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("columns", ctx_r2.columns)("columnOperands", ctx_r2.columnOperands)("rule", ctx_r2.rule.rules[i_r4]);
} }
function RuleComponent_ng_container_0_Template(rf, ctx) { if (rf & 1) {
    const _r10 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "ai-rule-builder-group-logic", 1);
    i0.ɵɵlistener("selectedChange", function RuleComponent_ng_container_0_Template_ai_rule_builder_group_logic_selectedChange_1_listener($event) { i0.ɵɵrestoreView(_r10); const ctx_r9 = i0.ɵɵnextContext(); return ctx_r9.rule.groupLogic = $event; });
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(2, RuleComponent_ng_container_0_ng_container_2_Template, 2, 3, "ng-container", 2);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r0 = i0.ɵɵnextContext();
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("id", ctx_r0.rule.id)("selected", ctx_r0.rule.groupLogic);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngForOf", ctx_r0.rule.rules);
} }
function RuleComponent_ng_container_1_input_5_Template(rf, ctx) { if (rf & 1) {
    const _r14 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "input", 14);
    i0.ɵɵlistener("ngModelChange", function RuleComponent_ng_container_1_input_5_Template_input_ngModelChange_0_listener($event) { i0.ɵɵrestoreView(_r14); const ctx_r13 = i0.ɵɵnextContext(2); return ctx_r13.rule.value = $event; });
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r11 = i0.ɵɵnextContext(2);
    i0.ɵɵproperty("ngModel", ctx_r11.rule.value);
} }
function RuleComponent_ng_container_1_6_ng_template_0_Template(rf, ctx) { }
const _c0 = function (a0) { return { $implicit: a0 }; };
function RuleComponent_ng_container_1_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵtemplate(0, RuleComponent_ng_container_1_6_ng_template_0_Template, 0, 0, "ng-template", 15);
} if (rf & 2) {
    const ctx_r12 = i0.ɵɵnextContext(2);
    i0.ɵɵproperty("ngTemplateOutlet", ctx_r12.getTemplate())("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, ctx_r12.rule));
} }
function RuleComponent_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    const _r17 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "ibm-dropdown", 4);
    i0.ɵɵlistener("ngModelChange", function RuleComponent_ng_container_1_Template_ibm_dropdown_ngModelChange_1_listener($event) { i0.ɵɵrestoreView(_r17); const ctx_r16 = i0.ɵɵnextContext(); return ctx_r16.rule.columnId = $event; });
    i0.ɵɵelement(2, "ibm-dropdown-list", 5);
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(3, "ibm-dropdown", 6);
    i0.ɵɵlistener("ngModelChange", function RuleComponent_ng_container_1_Template_ibm_dropdown_ngModelChange_3_listener($event) { i0.ɵɵrestoreView(_r17); const ctx_r18 = i0.ɵɵnextContext(); return ctx_r18.rule.operand = $event; });
    i0.ɵɵelement(4, "ibm-dropdown-list", 5);
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(5, RuleComponent_ng_container_1_input_5_Template, 1, 1, "input", 7);
    i0.ɵɵtemplate(6, RuleComponent_ng_container_1_6_Template, 1, 4, undefined, 0);
    i0.ɵɵelementStart(7, "div", 8);
    i0.ɵɵelementStart(8, "button", 9);
    i0.ɵɵlistener("click", function RuleComponent_ng_container_1_Template_button_click_8_listener() { i0.ɵɵrestoreView(_r17); const ctx_r19 = i0.ɵɵnextContext(); return ctx_r19.removeRule.emit(ctx_r19.rule.id); });
    i0.ɵɵnamespaceSVG();
    i0.ɵɵelement(9, "svg", 10);
    i0.ɵɵnamespaceHTML();
    i0.ɵɵelementStart(10, "span", 11);
    i0.ɵɵtext(11);
    i0.ɵɵelementEnd();
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(12, "button", 9);
    i0.ɵɵlistener("click", function RuleComponent_ng_container_1_Template_button_click_12_listener() { i0.ɵɵrestoreView(_r17); const ctx_r20 = i0.ɵɵnextContext(); return ctx_r20.addRule.emit({ id: ctx_r20.rule.id }); });
    i0.ɵɵnamespaceSVG();
    i0.ɵɵelement(13, "svg", 12);
    i0.ɵɵnamespaceHTML();
    i0.ɵɵelementStart(14, "span", 11);
    i0.ɵɵtext(15);
    i0.ɵɵelementEnd();
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(16, "button", 9);
    i0.ɵɵlistener("click", function RuleComponent_ng_container_1_Template_button_click_16_listener() { i0.ɵɵrestoreView(_r17); const ctx_r21 = i0.ɵɵnextContext(); return ctx_r21.addRule.emit({ id: ctx_r21.rule.id, isGroup: true }); });
    i0.ɵɵnamespaceSVG();
    i0.ɵɵelement(17, "svg", 13);
    i0.ɵɵnamespaceHTML();
    i0.ɵɵelementStart(18, "span", 11);
    i0.ɵɵtext(19);
    i0.ɵɵelementEnd();
    i0.ɵɵelementEnd();
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngModel", ctx_r1.rule.columnId);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("items", ctx_r1.getColumns());
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngModel", ctx_r1.rule.operand);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("items", ctx_r1.getColumnOperands());
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !ctx_r1.hasTemplate());
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r1.hasTemplate());
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("iconOnly", true);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(ctx_r1.removeRuleLabel);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("iconOnly", true);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(ctx_r1.addNewRuleLabel);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("iconOnly", true);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(ctx_r1.addNewGroupLabel);
} }
export class RuleComponent {
    constructor(i18n) {
        this.i18n = i18n;
        this.columns = [];
        this.columnOperands = [];
        this.removeRuleLabel = '';
        this.addNewRuleLabel = '';
        this.addNewGroupLabel = '';
        this.ruleChange = new EventEmitter();
        this.removeRule = new EventEmitter();
        this.addRule = new EventEmitter();
    }
    get isRule() {
        return this.rule && !this.rule.groupLogic && !Array.isArray(this.rule.rules);
    }
    get isRuleGroup() {
        return this.rule && this.rule.groupLogic && Array.isArray(this.rule.rules);
    }
    ngOnInit() {
        this.removeRuleLabel = this.removeRuleLabel || this.i18n.get().RULE_BUILDER.REMOVE_RULE;
        this.addNewRuleLabel = this.addNewRuleLabel || this.i18n.get().RULE_BUILDER.ADD_NEW_RULE;
        this.addNewGroupLabel = this.addNewGroupLabel || this.i18n.get().RULE_BUILDER.ADD_NEW_GROUP;
    }
    hasTemplate() {
        const selectedColumn = this.columns.find((column) => column.id === this.rule.columnId);
        return !!(selectedColumn === null || selectedColumn === void 0 ? void 0 : selectedColumn.valueTemplate);
    }
    getTemplate() {
        const selectedColumn = this.columns.find((column) => column.id === this.rule.columnId);
        return selectedColumn === null || selectedColumn === void 0 ? void 0 : selectedColumn.valueTemplate;
    }
    getColumnOperands() {
        const selectedColumn = this.columns.find((column) => column.id === this.rule.columnId);
        if (selectedColumn === null || selectedColumn === void 0 ? void 0 : selectedColumn.operands) {
            return selectedColumn.operands;
        }
        return this.columnOperands;
    }
    getColumns() {
        // we cache this because adding operands throws a "circular" error from cca
        // and doing it on the fly makes ngModel not work
        if (!this.dropdownColumns) {
            this.dropdownColumns = this.columns.map((column) => ({
                content: column.content,
                id: column.id,
                selected: column.selected,
            }));
        }
        return this.dropdownColumns;
    }
}
RuleComponent.ɵfac = function RuleComponent_Factory(t) { return new (t || RuleComponent)(i0.ɵɵdirectiveInject(i1.I18n)); };
RuleComponent.ɵcmp = i0.ɵɵdefineComponent({ type: RuleComponent, selectors: [["ai-rule"]], hostVars: 4, hostBindings: function RuleComponent_HostBindings(rf, ctx) { if (rf & 2) {
        i0.ɵɵclassProp("iot--rule-builder-rule", ctx.isRule)("iot--rule-builder-rule--group", ctx.isRuleGroup);
    } }, inputs: { columns: "columns", columnOperands: "columnOperands", removeRuleLabel: "removeRuleLabel", addNewRuleLabel: "addNewRuleLabel", addNewGroupLabel: "addNewGroupLabel", rule: "rule" }, outputs: { ruleChange: "ruleChange", removeRule: "removeRule", addRule: "addRule" }, decls: 2, vars: 2, consts: [[4, "ngIf"], [3, "id", "selected", "selectedChange"], [4, "ngFor", "ngForOf"], [3, "columns", "columnOperands", "rule", "addRule", "removeRule", "ruleChange"], ["theme", "light", "placeholder", "Select a column", "value", "id", 3, "ngModel", "ngModelChange"], [3, "items"], ["theme", "light", "placeholder", "Select an operand", "value", "id", 3, "ngModel", "ngModelChange"], ["ibmText", "", "theme", "light", "placeholder", "Enter a value", 3, "ngModel", "ngModelChange", 4, "ngIf"], [1, "iot--rule-builder-rule__actions"], ["ibmButton", "ghost", 3, "iconOnly", "click"], ["ibmIcon", "subtract", "size", "32", 1, "bx--btn__icon"], [1, "bx--assistive-text"], ["ibmIcon", "add", "size", "32", 1, "bx--btn__icon"], ["ibmIcon", "text--new-line", "size", "32", 1, "bx--btn__icon"], ["ibmText", "", "theme", "light", "placeholder", "Enter a value", 3, "ngModel", "ngModelChange"], [3, "ngTemplateOutlet", "ngTemplateOutletContext"]], template: function RuleComponent_Template(rf, ctx) { if (rf & 1) {
        i0.ɵɵtemplate(0, RuleComponent_ng_container_0_Template, 3, 3, "ng-container", 0);
        i0.ɵɵtemplate(1, RuleComponent_ng_container_1_Template, 20, 12, "ng-container", 0);
    } if (rf & 2) {
        i0.ɵɵproperty("ngIf", ctx.isRuleGroup);
        i0.ɵɵadvance(1);
        i0.ɵɵproperty("ngIf", ctx.isRule);
    } }, directives: [i2.NgIf, i3.RuleBuilderGroupLogicComponent, i2.NgForOf, RuleComponent, i1.Dropdown, i4.NgControlStatus, i4.NgModel, i1.DropdownList, i1.Button, i1.IconDirective, i4.DefaultValueAccessor, i1.TextInput, i2.NgTemplateOutlet], encapsulation: 2 });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(RuleComponent, [{
        type: Component,
        args: [{
                selector: 'ai-rule',
                template: `
    <ng-container *ngIf="isRuleGroup">
      <ai-rule-builder-group-logic [id]="rule.id" [(selected)]="rule.groupLogic">
      </ai-rule-builder-group-logic>
      <ng-container *ngFor="let r of rule.rules; let i = index">
        <ai-rule
          (addRule)="addRule.emit($event)"
          (removeRule)="removeRule.emit($event)"
          [columns]="columns"
          [columnOperands]="columnOperands"
          [(rule)]="rule.rules[i]"
        ></ai-rule>
      </ng-container>
    </ng-container>
    <ng-container *ngIf="isRule">
      <ibm-dropdown
        theme="light"
        placeholder="Select a column"
        [(ngModel)]="rule.columnId"
        value="id"
      >
        <ibm-dropdown-list [items]="getColumns()"></ibm-dropdown-list>
      </ibm-dropdown>
      <ibm-dropdown
        theme="light"
        placeholder="Select an operand"
        [(ngModel)]="rule.operand"
        value="id"
      >
        <ibm-dropdown-list [items]="getColumnOperands()"></ibm-dropdown-list>
      </ibm-dropdown>
      <input
        *ngIf="!hasTemplate()"
        ibmText
        theme="light"
        placeholder="Enter a value"
        [(ngModel)]="rule.value"
      />
      <ng-template
        *ngIf="hasTemplate()"
        [ngTemplateOutlet]="getTemplate()"
        [ngTemplateOutletContext]="{ $implicit: rule }"
      >
      </ng-template>
      <div class="iot--rule-builder-rule__actions">
        <button ibmButton="ghost" [iconOnly]="true" (click)="removeRule.emit(rule.id)">
          <svg class="bx--btn__icon" ibmIcon="subtract" size="32"></svg>
          <span class="bx--assistive-text">{{ removeRuleLabel }}</span>
        </button>
        <button ibmButton="ghost" [iconOnly]="true" (click)="addRule.emit({ id: rule.id })">
          <svg class="bx--btn__icon" ibmIcon="add" size="32"></svg>
          <span class="bx--assistive-text">{{ addNewRuleLabel }}</span>
        </button>
        <button
          ibmButton="ghost"
          [iconOnly]="true"
          (click)="addRule.emit({ id: rule.id, isGroup: true })"
        >
          <svg class="bx--btn__icon" ibmIcon="text--new-line" size="32"></svg>
          <span class="bx--assistive-text">{{ addNewGroupLabel }}</span>
        </button>
      </div>
    </ng-container>
  `,
            }]
    }], function () { return [{ type: i1.I18n }]; }, { columns: [{
            type: Input
        }], columnOperands: [{
            type: Input
        }], removeRuleLabel: [{
            type: Input
        }], addNewRuleLabel: [{
            type: Input
        }], addNewGroupLabel: [{
            type: Input
        }], rule: [{
            type: Input
        }], ruleChange: [{
            type: Output
        }], removeRule: [{
            type: Output
        }], addRule: [{
            type: Output
        }], isRule: [{
            type: HostBinding,
            args: ['class.iot--rule-builder-rule']
        }], isRuleGroup: [{
            type: HostBinding,
            args: ['class.iot--rule-builder-rule--group']
        }] }); })();
//# sourceMappingURL=data:application/json;base64, 