import { Component, Output, EventEmitter } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { ActionType } from './export-schedules.interface';
import { CronService } from './cron.service';
import { cloneDeep } from 'lodash-es';
import { gettext } from '@c8y/ngx-components';
import { ReportsService } from './reports.service';
import * as i0 from "@angular/core";
import * as i1 from "./reports.service";
import * as i2 from "ngx-bootstrap/modal";
import * as i3 from "./cron.service";
import * as i4 from "@c8y/ngx-components";
import * as i5 from "@angular/common";
import * as i6 from "@angular/forms";
import * as i7 from "./cron.component";
export class ScheduleModalComponent {
    constructor(reportsService, modalRef, cronService) {
        this.reportsService = reportsService;
        this.modalRef = modalRef;
        this.cronService = cronService;
        this.emitter = new EventEmitter();
        this.ActionType = ActionType;
        this.cronExpression = '* * * * *';
        this.validCron = false;
        this.emitterPayload = {
            success: false,
            message: '',
            schedule: { timestamp: undefined, emailConfig: undefined, cronConfig: undefined }
        };
        this.placeholdersInfo = gettext('Available placeholders: {tenant-domain}, {host}, {binaryId}. Whole link to downloadable file is: {tenant-domain}/inventory/binaries/{binaryId}.');
    }
    ngOnInit() {
        this.oldSchedule = cloneDeep(this.schedule);
        this.populateEmailFieldsFromSchedule(this.schedule);
        this.cronExpression = this.cronService.generateCron(this.schedule.cronConfig);
        this.validCron = this.cronService.validateModels(this.cronService.getBase(this.schedule.cronConfig), this.schedule.cronConfig);
    }
    populateEmailFieldsFromSchedule(schedule) {
        if (schedule.emailConfig.to && schedule.emailConfig.to.length) {
            this.emailTo = schedule.emailConfig.to.toString();
        }
        if (schedule.emailConfig.cc && schedule.emailConfig.cc.length) {
            this.emailCc = schedule.emailConfig.cc.toString();
        }
        if (schedule.emailConfig.bcc && schedule.emailConfig.bcc.length) {
            this.emailBcc = schedule.emailConfig.bcc.toString();
        }
        if (schedule.emailConfig.replyTo) {
            this.emailReplyTo = schedule.emailConfig.replyTo;
        }
        if (schedule.emailConfig.subject) {
            this.emailSubject = schedule.emailConfig.subject;
        }
        if (schedule.emailConfig.text) {
            this.emailText = schedule.emailConfig.text;
        }
    }
    save() {
        this.populateScheduleFromCronExpression();
        this.populateScheduleFromEmailFields();
        if (this.actionType === ActionType.CREATE || this.actionType === ActionType.DUPLICATE) {
            const date = new Date();
            const timestamp = date.getTime();
            this.schedule.timestamp = timestamp;
        }
        this.emitterPayload.success = true;
        this.emitterPayload.schedule = this.schedule;
        this.modalRef.hide();
        // signal to the parent component to update list
        this.emitter.emit(this.emitterPayload);
    }
    cancel() {
        this.modalRef.hide();
    }
    getCron(cron) {
        this.validCron = cron.valid;
        if (cron.valid) {
            this.cronExpression = cron.cron;
        }
    }
    populateScheduleFromCronExpression() {
        this.schedule.cronConfig = this.cronService.generateCronConfig(this.cronExpression);
    }
    convertStringOfEmailsToArray(stringOfEmails) {
        const arr = [];
        if (stringOfEmails) {
            const parts = stringOfEmails.split(',');
            parts.forEach(item => {
                if (item) {
                    arr.push(item);
                }
            });
        }
        return arr;
    }
    populateScheduleFromEmailFields() {
        this.schedule.emailConfig.to = this.emailTo
            ? this.convertStringOfEmailsToArray(this.emailTo)
            : null;
        this.schedule.emailConfig.cc = this.emailCc
            ? this.convertStringOfEmailsToArray(this.emailCc)
            : null;
        this.schedule.emailConfig.bcc = this.emailBcc
            ? this.convertStringOfEmailsToArray(this.emailBcc)
            : null;
        this.schedule.emailConfig.replyTo = this.emailReplyTo;
        this.schedule.emailConfig.subject = this.emailSubject;
        this.schedule.emailConfig.text = this.emailText;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ScheduleModalComponent, deps: [{ token: i1.ReportsService }, { token: i2.BsModalRef }, { token: i3.CronService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: ScheduleModalComponent, selector: "schedule-modal", outputs: { emitter: "emitter" }, ngImport: i0, template: "<div class=\"modal-header dialog-header\">\n  <i c8yIcon=\"c8y-report\"></i>\n  <h4 id=\"modal-title\">\n    <span *ngIf=\"actionType === ActionType.CREATE\" translate>New export schedule</span>\n    <span *ngIf=\"actionType === ActionType.EDIT\" translate>Edit export schedule</span>\n    <span *ngIf=\"actionType === ActionType.DUPLICATE\" translate>Duplicate export schedule</span>\n  </h4>\n</div>\n\n<div class=\"modal-body\" id=\"modal-body\">\n  <p class=\"lead text-center p-t-24 m-b-0\" translate>On schedule send export via email</p>\n</div>\n<div class=\"modal-inner-scroll smart-rule-control\">\n  <form #scheduleForm=\"ngForm\" class=\"edit-smart-rule-details\">\n    <div class=\"list-group\">\n      <div class=\"list-group-item bg-level-1\">\n        <div class=\"smart-list-icon-label\">\n          <span class=\"dot bg-primary-light m-r-8\">1</span>\n          <strong translate>Frequency</strong>\n        </div>\n        <div class=\"p-t-16\">\n          <div class=\"form-group\">\n            <cron [cronIn]=\"cronExpression\" (emitter)=\"getCron($event)\" name=\"cron\"></cron>\n          </div>\n        </div>\n      </div>\n      <div class=\"list-group-item\">\n        <div class=\"smart-list-icon-label\">\n          <span class=\"dot bg-primary-light m-r-8\">2</span>\n          <div class=\"d-inline-block\">\n            <strong translate>Send email</strong>\n            <p class=\"help-block text-muted small p-absolute\">\n              <i class=\"text-info m-r-4 text-14\" c8yIcon=\"info-circle\"></i>\n              <span translate\n                >Enter one or more valid email addresses, separated with a comma.</span\n              >\n            </p>\n          </div>\n        </div>\n        <div class=\"p-t-24\">\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Send to</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control\"\n                name=\"to\"\n                [(ngModel)]=\"emailTo\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                required\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>CC</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"cc\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                [(ngModel)]=\"emailCc\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>BCC</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"bcc\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                [(ngModel)]=\"emailBcc\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Reply to (single email address)</label>\n            <c8y-form-group>\n              <input\n                email\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"replyTo\"\n                placeholder=\"{{ 'e.g. joe.doe@example.com`LOCALIZE`' | translate }}\"\n                [(ngModel)]=\"emailReplyTo\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Subject</label>\n            <c8y-form-group>\n              <input\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"subject\"\n                [(ngModel)]=\"emailSubject\"\n                placeholder=\"{{ 'e.g. Daily report' | translate }}\"\n                required\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Message</label>\n            <c8y-form-group>\n              <textarea\n                class=\"form-control\"\n                name=\"text\"\n                [(ngModel)]=\"emailText\"\n                placeholder=\"{{ 'Message' | translate }}\"\n                rows=\"4\"\n                required\n              ></textarea>\n              <p class=\"help-block text-muted\">\n                {{ placeholdersInfo | translate }}\n              </p>\n            </c8y-form-group>\n          </div>\n        </div>\n      </div>\n    </div>\n  </form>\n</div>\n\n<div class=\"modal-footer\">\n  <button class=\"btn btn-default\" (click)=\"cancel()\" title=\"{{ 'Cancel' | translate }}\">\n    {{ 'Cancel' | translate }}\n  </button>\n  <button\n    class=\"btn btn-primary\"\n    (click)=\"save()\"\n    [disabled]=\"!validCron || !scheduleForm.form.valid\"\n  >\n    <span>\n      <span *ngIf=\"actionType === ActionType.CREATE\" title=\"{{ 'Create' | translate }}\">\n        {{ 'Create' | translate }}\n      </span>\n      <span *ngIf=\"actionType === ActionType.EDIT\" title=\"{{ 'Save' | translate }}\">\n        {{ 'Save' | translate }}\n      </span>\n      <span *ngIf=\"actionType === ActionType.DUPLICATE\" title=\"{{ 'Duplicate' | translate }}\">\n        {{ 'Duplicate' | translate }}\n      </span>\n    </span>\n  </button>\n</div>\n", dependencies: [{ kind: "directive", type: i4.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i4.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i6.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i6.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i6.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i6.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i6.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i6.EmailValidator, selector: "[email][formControlName],[email][formControl],[email][ngModel]", inputs: ["email"] }, { kind: "directive", type: i6.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i6.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i4.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i4.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i4.EmailsValidatorDirective, selector: "[emails][formControlName],[emails][formControl],[emails][ngModel]", inputs: ["emails"] }, { kind: "component", type: i7.CronComponent, selector: "cron", inputs: ["cronIn"], outputs: ["emitter"] }, { kind: "pipe", type: i4.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ScheduleModalComponent, decorators: [{
            type: Component,
            args: [{ selector: 'schedule-modal', template: "<div class=\"modal-header dialog-header\">\n  <i c8yIcon=\"c8y-report\"></i>\n  <h4 id=\"modal-title\">\n    <span *ngIf=\"actionType === ActionType.CREATE\" translate>New export schedule</span>\n    <span *ngIf=\"actionType === ActionType.EDIT\" translate>Edit export schedule</span>\n    <span *ngIf=\"actionType === ActionType.DUPLICATE\" translate>Duplicate export schedule</span>\n  </h4>\n</div>\n\n<div class=\"modal-body\" id=\"modal-body\">\n  <p class=\"lead text-center p-t-24 m-b-0\" translate>On schedule send export via email</p>\n</div>\n<div class=\"modal-inner-scroll smart-rule-control\">\n  <form #scheduleForm=\"ngForm\" class=\"edit-smart-rule-details\">\n    <div class=\"list-group\">\n      <div class=\"list-group-item bg-level-1\">\n        <div class=\"smart-list-icon-label\">\n          <span class=\"dot bg-primary-light m-r-8\">1</span>\n          <strong translate>Frequency</strong>\n        </div>\n        <div class=\"p-t-16\">\n          <div class=\"form-group\">\n            <cron [cronIn]=\"cronExpression\" (emitter)=\"getCron($event)\" name=\"cron\"></cron>\n          </div>\n        </div>\n      </div>\n      <div class=\"list-group-item\">\n        <div class=\"smart-list-icon-label\">\n          <span class=\"dot bg-primary-light m-r-8\">2</span>\n          <div class=\"d-inline-block\">\n            <strong translate>Send email</strong>\n            <p class=\"help-block text-muted small p-absolute\">\n              <i class=\"text-info m-r-4 text-14\" c8yIcon=\"info-circle\"></i>\n              <span translate\n                >Enter one or more valid email addresses, separated with a comma.</span\n              >\n            </p>\n          </div>\n        </div>\n        <div class=\"p-t-24\">\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Send to</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control\"\n                name=\"to\"\n                [(ngModel)]=\"emailTo\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                required\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>CC</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"cc\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                [(ngModel)]=\"emailCc\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>BCC</label>\n            <c8y-form-group>\n              <input\n                emails\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"bcc\"\n                placeholder=\"{{\n                  'e.g. joe.doe@example.com,john.smith@example.com`LOCALIZE`' | translate\n                }}\"\n                [(ngModel)]=\"emailBcc\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Reply to (single email address)</label>\n            <c8y-form-group>\n              <input\n                email\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"replyTo\"\n                placeholder=\"{{ 'e.g. joe.doe@example.com`LOCALIZE`' | translate }}\"\n                [(ngModel)]=\"emailReplyTo\"\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Subject</label>\n            <c8y-form-group>\n              <input\n                type=\"text\"\n                class=\"form-control span\"\n                name=\"subject\"\n                [(ngModel)]=\"emailSubject\"\n                placeholder=\"{{ 'e.g. Daily report' | translate }}\"\n                required\n              />\n            </c8y-form-group>\n          </div>\n\n          <div class=\"form-group\">\n            <label class=\"control-label\" translate>Message</label>\n            <c8y-form-group>\n              <textarea\n                class=\"form-control\"\n                name=\"text\"\n                [(ngModel)]=\"emailText\"\n                placeholder=\"{{ 'Message' | translate }}\"\n                rows=\"4\"\n                required\n              ></textarea>\n              <p class=\"help-block text-muted\">\n                {{ placeholdersInfo | translate }}\n              </p>\n            </c8y-form-group>\n          </div>\n        </div>\n      </div>\n    </div>\n  </form>\n</div>\n\n<div class=\"modal-footer\">\n  <button class=\"btn btn-default\" (click)=\"cancel()\" title=\"{{ 'Cancel' | translate }}\">\n    {{ 'Cancel' | translate }}\n  </button>\n  <button\n    class=\"btn btn-primary\"\n    (click)=\"save()\"\n    [disabled]=\"!validCron || !scheduleForm.form.valid\"\n  >\n    <span>\n      <span *ngIf=\"actionType === ActionType.CREATE\" title=\"{{ 'Create' | translate }}\">\n        {{ 'Create' | translate }}\n      </span>\n      <span *ngIf=\"actionType === ActionType.EDIT\" title=\"{{ 'Save' | translate }}\">\n        {{ 'Save' | translate }}\n      </span>\n      <span *ngIf=\"actionType === ActionType.DUPLICATE\" title=\"{{ 'Duplicate' | translate }}\">\n        {{ 'Duplicate' | translate }}\n      </span>\n    </span>\n  </button>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.ReportsService }, { type: i2.BsModalRef }, { type: i3.CronService }], propDecorators: { emitter: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 