/**
 * SuiteCRM is a customer relationship management program developed by SalesAgility Ltd.
 * Copyright (C) 2021 SalesAgility Ltd.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SALESAGILITY, SALESAGILITY DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Supercharged by SuiteCRM" logo. If the display of the logos is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Supercharged by SuiteCRM".
 */
import { Component } from '@angular/core';
import { DataTypeFormatter } from '../../services/formatters/data-type.formatter.service';
import { RecordManager } from '../../services/record/record.manager';
import { FieldLogicManager } from '../field-logic/field-logic.manager';
import { BaseLineItemsComponent } from '../base/base-line-items.component';
import { FieldManager } from '../../services/record/field/field.manager';
import { FieldRegistry } from '../field.registry';
import { FieldLogicDisplayManager } from '../field-logic-display/field-logic-display.manager';
import * as i0 from "@angular/core";
import * as i1 from "../../services/formatters/data-type.formatter.service";
import * as i2 from "../field.registry";
import * as i3 from "../../services/record/record.manager";
import * as i4 from "../field-logic/field-logic.manager";
import * as i5 from "../../services/record/field/field.manager";
import * as i6 from "../field-logic-display/field-logic-display.manager";
import * as i7 from "@angular/common";
import * as i8 from "../../components/label/label.component";
import * as i9 from "../dynamic-field/dynamic-field.component";
import * as i10 from "../../components/button/button.component";
import * as i11 from "../../components/dynamic-label/dynamic-label.component";
function LineItemsComponent_ng_container_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelement(1, "scrm-label", 1);
    i0.ɵɵelementContainerEnd();
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_div_2_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div");
    i0.ɵɵelement(1, "scrm-dynamic-field", 10);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const itemField_r12 = ctx.$implicit;
    const item_r7 = i0.ɵɵnextContext(2).$implicit;
    const ctx_r10 = i0.ɵɵnextContext(3);
    i0.ɵɵclassMapInterpolate2("", itemField_r12.type, " ", itemField_r12.name, "");
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("componentType", ctx_r10.getComponentType(itemField_r12.type, itemField_r12.definition))("field", itemField_r12)("klass", ctx_r10.klass)("mode", ctx_r10.mode)("originalMode", ctx_r10.originalMode)("record", item_r7)("parent", ctx_r10.parent)("type", itemField_r12.type);
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_scrm_button_4_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelement(0, "scrm-button", 11);
} if (rf & 2) {
    const ctx_r14 = i0.ɵɵnextContext(2);
    const item_r7 = ctx_r14.$implicit;
    const i_r8 = ctx_r14.index;
    const ctx_r11 = i0.ɵɵnextContext(3);
    i0.ɵɵproperty("config", ctx_r11.getRemoveItemButton(item_r7, i_r8));
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 6)(1, "div", 7);
    i0.ɵɵtemplate(2, LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_div_2_Template, 2, 12, "div", 8);
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(3, "div", 9);
    i0.ɵɵtemplate(4, LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_scrm_button_4_Template, 1, 1, "scrm-button", 4);
    i0.ɵɵelementEnd()();
} if (rf & 2) {
    const item_r7 = i0.ɵɵnextContext().$implicit;
    const ctx_r9 = i0.ɵɵnextContext(3);
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngForOf", ctx_r9.getItemFields(item_r7));
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngIf", ctx_r9.isEditable());
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_div_1_Template, 5, 2, "div", 5);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const item_r7 = ctx.$implicit;
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !(item_r7 && item_r7.attributes && item_r7.attributes.deleted));
} }
function LineItemsComponent_ng_container_1_ng_container_1_scrm_button_5_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelement(0, "scrm-button", 11);
} if (rf & 2) {
    const ctx_r5 = i0.ɵɵnextContext(3);
    i0.ɵɵproperty("config", ctx_r5.getAddItemButton());
} }
const _c0 = function (a0) { return { field: a0 }; };
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_ng_container_1_div_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 13);
    i0.ɵɵelement(1, "scrm-dynamic-label", 14);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const item_r18 = ctx.$implicit;
    const ctx_r17 = i0.ɵɵnextContext(5);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("context", ctx_r17.getMessageContext(item_r18.value, ctx_r17.record))("fields", i0.ɵɵpureFunction1(3, _c0, ctx_r17.field))("labelKey", ctx_r17.getMessageLabelKey(item_r18.value));
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_ng_container_1_div_1_Template, 2, 5, "div", 12);
    i0.ɵɵpipe(2, "keyvalue");
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r16 = i0.ɵɵnextContext(4);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngForOf", i0.ɵɵpipeBind1(2, 1, ctx_r16.field.itemFormArray.errors));
} }
function LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_ng_container_1_Template, 3, 3, "ng-container", 0);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r6 = i0.ɵɵnextContext(3);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r6.field.itemFormArray.invalid);
} }
function LineItemsComponent_ng_container_1_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "div")(2, "div");
    i0.ɵɵtemplate(3, LineItemsComponent_ng_container_1_ng_container_1_ng_container_3_Template, 2, 1, "ng-container", 2);
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(4, "div", 3);
    i0.ɵɵtemplate(5, LineItemsComponent_ng_container_1_ng_container_1_scrm_button_5_Template, 1, 1, "scrm-button", 4);
    i0.ɵɵelementEnd()();
    i0.ɵɵtemplate(6, LineItemsComponent_ng_container_1_ng_container_1_ng_container_6_Template, 2, 1, "ng-container", 0);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r2 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance(1);
    i0.ɵɵclassMapInterpolate2("line-items d-flex flex-column ", ctx_r2.field.type, " ", ctx_r2.field.name, "");
    i0.ɵɵadvance(1);
    i0.ɵɵclassMapInterpolate1("d-flex ", ctx_r2.getDirection(), " justify-content-start align-items-end line-item h-100");
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngForOf", ctx_r2.field.items);
    i0.ɵɵadvance(2);
    i0.ɵɵproperty("ngIf", ctx_r2.isEditable());
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", (ctx_r2.mode === "edit" || ctx_r2.mode === "filter") && ctx_r2.field.itemFormArray && ctx_r2.field.itemFormArray.errors);
} }
function LineItemsComponent_ng_container_1_ng_container_2_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵelementStart(1, "div", 15);
    i0.ɵɵelement(2, "div", 16);
    i0.ɵɵelementEnd();
    i0.ɵɵelementContainerEnd();
} }
function LineItemsComponent_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementContainerStart(0);
    i0.ɵɵtemplate(1, LineItemsComponent_ng_container_1_ng_container_1_Template, 7, 10, "ng-container", 0);
    i0.ɵɵtemplate(2, LineItemsComponent_ng_container_1_ng_container_2_Template, 3, 0, "ng-container", 0);
    i0.ɵɵelementContainerEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !ctx_r1.field.loading);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r1.field.loading);
} }
class LineItemsComponent extends BaseLineItemsComponent {
    constructor(typeFormatter, registry, recordManager, logic, fieldManager, logicDisplay) {
        super(typeFormatter, registry, recordManager, logic, fieldManager, logicDisplay);
        this.typeFormatter = typeFormatter;
        this.registry = registry;
        this.recordManager = recordManager;
        this.logic = logic;
        this.fieldManager = fieldManager;
        this.logicDisplay = logicDisplay;
    }
    ngOnInit() {
        super.ngOnInit();
        this.field.metadata = this?.field?.metadata ?? {};
        const emptyItemInitialized = this?.field?.metadata?.emptyItemInitialized ?? false;
        if (['create'].includes(this.originalMode) && !emptyItemInitialized) {
            this.initEmptyItem();
            this.field.metadata.emptyItemInitialized = true;
        }
    }
    /**
     * Add item button config
     * @returns {object} ButtonInterface
     */
    getAddItemButton() {
        return {
            klass: 'btn btn-sm btn-outline-secondary m-0 border-0',
            icon: 'plus',
            onClick: () => {
                this.addEmptyItem();
            },
        };
    }
    /**
     * Remove item button config
     * @param {object} item
     * @param {number} index
     * @returns {object} ButtonInterface
     */
    getRemoveItemButton(item, index) {
        return {
            klass: 'btn btn-sm btn-outline-secondary m-0 border-0',
            icon: 'minimise',
            onClick: () => {
                this.removeItem(index);
            },
        };
    }
    static { this.ɵfac = function LineItemsComponent_Factory(t) { return new (t || LineItemsComponent)(i0.ɵɵdirectiveInject(i1.DataTypeFormatter), i0.ɵɵdirectiveInject(i2.FieldRegistry), i0.ɵɵdirectiveInject(i3.RecordManager), i0.ɵɵdirectiveInject(i4.FieldLogicManager), i0.ɵɵdirectiveInject(i5.FieldManager), i0.ɵɵdirectiveInject(i6.FieldLogicDisplayManager)); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: LineItemsComponent, selectors: [["scrm-line-items-field"]], features: [i0.ɵɵInheritDefinitionFeature], decls: 2, vars: 2, consts: [[4, "ngIf"], ["labelKey", "LBL_LINE_ITEMS_FIELD_CONFIG"], [4, "ngFor", "ngForOf"], [1, "line-item-buttons", "d-flex", "justify-content-end", "mt-1"], [3, "config", 4, "ngIf"], ["class", "d-flex flex-row line-item-entry w-100 align-items-end", 4, "ngIf"], [1, "d-flex", "flex-row", "line-item-entry", "w-100", "align-items-end"], [1, "flex-grow-1", "line-item-entry-composite"], [3, "class", 4, "ngFor", "ngForOf"], [1, "line-item-entry-buttons", "mb-1"], [3, "componentType", "field", "klass", "mode", "originalMode", "record", "parent", "type"], [3, "config"], ["class", "invalid-feedback d-block", 4, "ngFor", "ngForOf"], [1, "invalid-feedback", "d-block"], [3, "context", "fields", "labelKey"], [1, "dynamic-field"], [1, "flex-grow-1", "text-break", "rounded", "box-loading", "skeleton-field-content"]], template: function LineItemsComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵtemplate(0, LineItemsComponent_ng_container_0_Template, 2, 0, "ng-container", 0);
            i0.ɵɵtemplate(1, LineItemsComponent_ng_container_1_Template, 3, 2, "ng-container", 0);
        } if (rf & 2) {
            i0.ɵɵproperty("ngIf", !ctx.isConfigured());
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", ctx.isConfigured());
        } }, dependencies: [i7.NgForOf, i7.NgIf, i8.LabelComponent, i9.DynamicFieldComponent, i10.ButtonComponent, i11.DynamicLabelComponent, i7.KeyValuePipe], encapsulation: 2 }); }
}
export { LineItemsComponent };
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(LineItemsComponent, [{
        type: Component,
        args: [{ selector: 'scrm-line-items-field', template: "<! --\n/**\n* SuiteCRM is a customer relationship management program developed by SalesAgility Ltd.\n* Copyright (C) 2021 SalesAgility Ltd.\n*\n* This program is free software; you can redistribute it and/or modify it under\n* the terms of the GNU Affero General Public License version 3 as published by the\n* Free Software Foundation with the addition of the following permission added\n* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK\n* IN WHICH THE COPYRIGHT IS OWNED BY SALESAGILITY, SALESAGILITY DISCLAIMS THE\n* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.\n*\n* This program is distributed in the hope that it will be useful, but WITHOUT\n* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more\n* details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program.  If not, see http://www.gnu.org/licenses.\n*\n* In accordance with Section 7(b) of the GNU Affero General Public License\n* version 3, these Appropriate Legal Notices must retain the display of the\n* \"Supercharged by SuiteCRM\" logo. If the display of the logos is not reasonably\n* feasible for technical reasons, the Appropriate Legal Notices must display\n* the words \"Supercharged by SuiteCRM\".\n*/\n-->\n<ng-container *ngIf=\"!isConfigured()\">\n    <scrm-label labelKey=\"LBL_LINE_ITEMS_FIELD_CONFIG\"></scrm-label>\n</ng-container>\n\n<ng-container *ngIf=\"isConfigured()\">\n\n    <ng-container *ngIf=\"!field.loading\">\n        <div class=\"line-items d-flex flex-column {{field.type}} {{field.name}}\">\n\n            <div class=\"d-flex {{getDirection()}} justify-content-start align-items-end line-item h-100\">\n                <ng-container *ngFor=\"let item of field.items; index as i\">\n                    <div *ngIf=\"!(item && item.attributes && item.attributes.deleted)\"\n                         class=\"d-flex flex-row line-item-entry w-100 align-items-end\">\n\n                        <div class=\"flex-grow-1 line-item-entry-composite\">\n                            <div *ngFor=\"let itemField of getItemFields(item)\"\n                                 class=\"{{itemField.type}} {{itemField.name}}\">\n                                <scrm-dynamic-field [componentType]=\"getComponentType(itemField.type, itemField.definition)\"\n                                                    [field]=\"itemField\"\n                                                    [klass]=\"klass\"\n                                                    [mode]=\"mode\"\n                                                    [originalMode]=\"originalMode\"\n                                                    [record]=\"item\"\n                                                    [parent]=\"parent\"\n                                                    [type]=\"itemField.type\">\n                                </scrm-dynamic-field>\n                            </div>\n                        </div>\n\n                        <div class=\"line-item-entry-buttons mb-1\">\n                            <scrm-button *ngIf=\"isEditable()\" [config]=\"getRemoveItemButton(item, i)\">\n                            </scrm-button>\n                        </div>\n                    </div>\n                </ng-container>\n\n\n            </div>\n            <div class=\"line-item-buttons d-flex justify-content-end mt-1\">\n                <scrm-button *ngIf=\"isEditable()\" [config]=\"getAddItemButton()\">\n                </scrm-button>\n            </div>\n\n        </div>\n\n        <ng-container *ngIf=\"(mode === 'edit' || mode === 'filter') && field.itemFormArray && field.itemFormArray.errors\">\n            <ng-container *ngIf=\"field.itemFormArray.invalid\">\n                <div *ngFor=\"let item of field.itemFormArray.errors | keyvalue\" class=\"invalid-feedback d-block\">\n                    <scrm-dynamic-label [context]=\"getMessageContext(item.value, record)\"\n                                        [fields]=\"{field: field}\"\n                                        [labelKey]=\"getMessageLabelKey(item.value)\">\n                    </scrm-dynamic-label>\n                </div>\n            </ng-container>\n        </ng-container>\n    </ng-container>\n\n\n    <ng-container *ngIf=\"field.loading\">\n        <div class= \"dynamic-field\">\n            <div class=\"flex-grow-1 text-break rounded box-loading skeleton-field-content\"></div>\n        </div>\n    </ng-container>\n\n</ng-container>\n" }]
    }], function () { return [{ type: i1.DataTypeFormatter }, { type: i2.FieldRegistry }, { type: i3.RecordManager }, { type: i4.FieldLogicManager }, { type: i5.FieldManager }, { type: i6.FieldLogicDisplayManager }]; }, null); })();
//# sourceMappingURL=data:application/json;base64, 