import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { AbstractAppConfig } from '../../../app.config';
import { OrderService } from '../../services/order/order.service';
import * as i0 from "@angular/core";
import * as i1 from "../../../app.config";
import * as i2 from "@angular/forms";
import * as i3 from "../../services/order/order.service";
import * as i4 from "@angular/common";
import * as i5 from "rpx-xui-translation";
const _c0 = a0 => ({ "EventTrigger-empty": a0 });
function EventTriggerComponent_form_0_option_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "option", 8);
    i0.ɵɵtext(1);
    i0.ɵɵpipe(2, "rpxTranslate");
    i0.ɵɵelementEnd();
} if (rf & 2) {
    i0.ɵɵadvance();
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(2, 1, "Select action"));
} }
function EventTriggerComponent_form_0_option_7_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "option", 9);
    i0.ɵɵtext(1);
    i0.ɵɵpipe(2, "rpxTranslate");
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const trigger_r3 = ctx.$implicit;
    i0.ɵɵproperty("ngValue", trigger_r3)("title", trigger_r3.description);
    i0.ɵɵadvance();
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(2, 3, trigger_r3.name));
} }
function EventTriggerComponent_form_0_Template(rf, ctx) { if (rf & 1) {
    const _r1 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "form", 1);
    i0.ɵɵlistener("ngSubmit", function EventTriggerComponent_form_0_Template_form_ngSubmit_0_listener() { i0.ɵɵrestoreView(_r1); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.triggerSubmit()); });
    i0.ɵɵelementStart(1, "div", 2)(2, "label", 3);
    i0.ɵɵtext(3);
    i0.ɵɵpipe(4, "rpxTranslate");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(5, "select", 4);
    i0.ɵɵlistener("change", function EventTriggerComponent_form_0_Template_select_change_5_listener() { i0.ɵɵrestoreView(_r1); const ctx_r1 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r1.triggerChange()); });
    i0.ɵɵtemplate(6, EventTriggerComponent_form_0_option_6_Template, 3, 3, "option", 5)(7, EventTriggerComponent_form_0_option_7_Template, 3, 5, "option", 6);
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(8, "button", 7);
    i0.ɵɵtext(9);
    i0.ɵɵpipe(10, "rpxTranslate");
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵproperty("formGroup", ctx_r1.triggerForm);
    i0.ɵɵadvance(3);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(4, 8, "Next step"));
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngClass", i0.ɵɵpureFunction1(12, _c0, !ctx_r1.triggerForm.value["trigger"]))("compareWith", ctx_r1.compareFn);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngIf", 1 !== ctx_r1.triggers.length);
    i0.ɵɵadvance();
    i0.ɵɵproperty("ngForOf", ctx_r1.triggers);
    i0.ɵɵadvance();
    i0.ɵɵproperty("disabled", ctx_r1.isButtonDisabled());
    i0.ɵɵadvance();
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(10, 10, ctx_r1.triggerText));
} }
export class EventTriggerComponent {
    appConfig;
    fb;
    orderService;
    triggers;
    triggerText;
    isDisabled;
    eventId = '';
    onTriggerSubmit = new EventEmitter();
    onTriggerChange = new EventEmitter();
    triggerForm;
    constructor(appConfig, fb, orderService) {
        this.appConfig = appConfig;
        this.fb = fb;
        this.orderService = orderService;
    }
    ngOnInit() {
        this.triggers = this.orderService.sort(this.triggers);
        if (this.eventId) {
            const eventBundle = this.triggers.find(ev => ev.id === this.eventId);
            this.triggerForm.controls['trigger'].patchValue({ id: eventBundle?.id, name: eventBundle?.name, description: eventBundle?.description });
        }
    }
    ngOnChanges(changes) {
        if (changes?.triggers?.currentValue) {
            const eventsToHide = this.appConfig.getEventsToHide();
            const filteredTriggers = this.triggers
                ?.filter((event) => !eventsToHide || !eventsToHide.includes(event.id));
            this.triggers = this.orderService.sort(filteredTriggers);
            this.triggerForm = this.fb.group({
                trigger: [this.getDefault(), Validators.required]
            });
        }
    }
    compareFn(c1, c2) {
        return c1 && c2 ? c1.id === c2.id : c1 === c2;
    }
    isButtonDisabled() {
        return !this.triggerForm.valid || this.isDisabled;
    }
    triggerSubmit() {
        this.onTriggerSubmit.emit(this.triggerForm.value['trigger']);
    }
    triggerChange() {
        this.isDisabled = false;
        this.onTriggerChange.emit(null);
    }
    getDefault() {
        return this.triggers?.length === 1 ? this.triggers[0] : '';
    }
    static ɵfac = function EventTriggerComponent_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || EventTriggerComponent)(i0.ɵɵdirectiveInject(i1.AbstractAppConfig), i0.ɵɵdirectiveInject(i2.FormBuilder), i0.ɵɵdirectiveInject(i3.OrderService)); };
    static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: EventTriggerComponent, selectors: [["ccd-event-trigger"]], inputs: { triggers: "triggers", triggerText: "triggerText", isDisabled: "isDisabled", eventId: "eventId" }, outputs: { onTriggerSubmit: "onTriggerSubmit", onTriggerChange: "onTriggerChange" }, features: [i0.ɵɵNgOnChangesFeature], decls: 1, vars: 1, consts: [["class", "event-trigger", 3, "formGroup", "ngSubmit", 4, "ngIf"], [1, "event-trigger", 3, "ngSubmit", "formGroup"], [1, "form-group", 2, "width", "350px !important"], ["for", "next-step", 1, "form-label"], ["id", "next-step", "formControlName", "trigger", 1, "form-control", "ccd-dropdown", 3, "change", "ngClass", "compareWith"], ["value", "", "data-default", "", 4, "ngIf"], [3, "ngValue", "title", 4, "ngFor", "ngForOf"], ["type", "submit", 1, "button", 3, "disabled"], ["value", "", "data-default", ""], [3, "ngValue", "title"]], template: function EventTriggerComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, EventTriggerComponent_form_0_Template, 11, 14, "form", 0);
        } if (rf & 2) {
            i0.ɵɵproperty("ngIf", ctx.triggers && ctx.triggers.length);
        } }, dependencies: [i4.NgClass, i4.NgForOf, i4.NgIf, i2.ɵNgNoValidate, i2.NgSelectOption, i2.ɵNgSelectMultipleOption, i2.SelectControlValueAccessor, i2.NgControlStatus, i2.NgControlStatusGroup, i2.FormGroupDirective, i2.FormControlName, i5.RpxTranslatePipe], styles: [".event-trigger[_ngcontent-%COMP%]{width:auto;margin-top:40px;margin-bottom:20px}.event-trigger[_ngcontent-%COMP%]   .form-group[_ngcontent-%COMP%]{margin-top:3px;margin-right:10px;margin-bottom:0;float:left;text-align:right;width:325px}.event-trigger[_ngcontent-%COMP%]   .form-group[_ngcontent-%COMP%]   .form-label[_ngcontent-%COMP%]{float:left;margin-top:5px}.event-trigger[_ngcontent-%COMP%]   select[_ngcontent-%COMP%]{width:250px}.event-trigger[_ngcontent-%COMP%]   select.EventTrigger-empty[_ngcontent-%COMP%], .event-trigger[_ngcontent-%COMP%]   select[_ngcontent-%COMP%]   [data-default][_ngcontent-%COMP%]{color:#6f777b}"] });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(EventTriggerComponent, [{
        type: Component,
        args: [{ selector: 'ccd-event-trigger', template: "<form *ngIf=\"triggers && triggers.length\" class=\"event-trigger\" (ngSubmit)=\"triggerSubmit()\" [formGroup]=\"triggerForm\">\n  <div class=\"form-group\" style=\"width: 350px !important;\">\n    <label class=\"form-label\" for=\"next-step\">{{'Next step' | rpxTranslate}}</label>\n    <select class=\"form-control ccd-dropdown\" id=\"next-step\" (change)=\"triggerChange()\" formControlName=\"trigger\" [ngClass]=\"{\n          'EventTrigger-empty': !triggerForm.value['trigger']\n        }\" [compareWith]=\"compareFn\">\n      <option *ngIf=\"1 !== triggers.length\" value=\"\" data-default>{{'Select action' | rpxTranslate}}</option>\n      <option *ngFor=\"let trigger of triggers\" [ngValue]=\"trigger\"  [title]=\"trigger.description\">{{trigger.name | rpxTranslate}}</option>\n    </select>\n  </div>\n  <button [disabled]=\"isButtonDisabled()\" type=\"submit\" class=\"button\">{{triggerText | rpxTranslate}}</button>\n</form>\n", styles: [".event-trigger{width:auto;margin-top:40px;margin-bottom:20px}.event-trigger .form-group{margin-top:3px;margin-right:10px;margin-bottom:0;float:left;text-align:right;width:325px}.event-trigger .form-group .form-label{float:left;margin-top:5px}.event-trigger select{width:250px}.event-trigger select.EventTrigger-empty,.event-trigger select [data-default]{color:#6f777b}\n"] }]
    }], () => [{ type: i1.AbstractAppConfig }, { type: i2.FormBuilder }, { type: i3.OrderService }], { triggers: [{
            type: Input
        }], triggerText: [{
            type: Input
        }], isDisabled: [{
            type: Input
        }], eventId: [{
            type: Input
        }], onTriggerSubmit: [{
            type: Output
        }], onTriggerChange: [{
            type: Output
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(EventTriggerComponent, { className: "EventTriggerComponent", filePath: "lib/shared/components/event-trigger/event-trigger.component.ts", lineNumber: 12 }); })();
//# sourceMappingURL=data:application/json;base64, 