var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from '@angular/core';
import { FieldsUtils } from '../../../services/fields/fields.utils';
// @dynamic
var PlaceholderService = /** @class */ (function () {
    function PlaceholderService(fieldsUtils) {
        this.fieldsUtils = fieldsUtils;
    }
    PlaceholderService_1 = PlaceholderService;
    PlaceholderService.prototype.resolvePlaceholders = function (pageFormFields, stringToResolve) {
        var startSubstitutionIndex = -1;
        var fieldIdToSubstitute = '';
        var isCollecting = false;
        var originalStringToResolve = stringToResolve;
        var numberCollectionItemsAsPlaceholder = 1;
        var colItemIndex = 0;
        var scanIndex = 0;
        if (stringToResolve && typeof stringToResolve === 'string') {
            while (numberCollectionItemsAsPlaceholder-- > 0) {
                while (scanIndex < stringToResolve.length) {
                    if (this.isStartPlaceholderAndNotCollecting(stringToResolve, scanIndex, isCollecting)) {
                        startSubstitutionIndex = scanIndex;
                        isCollecting = true;
                    }
                    else if (isCollecting) {
                        if (this.isClosingPlaceholder(stringToResolve, scanIndex)) {
                            if (this.isMatchingPlaceholderPattern(fieldIdToSubstitute)
                                && this.isFieldIdInFormFields(fieldIdToSubstitute, pageFormFields, colItemIndex)) {
                                if (fieldIdToSubstitute.split('.').length > 1) {
                                    var newNumberOfCollectionItemsAsPlaceholder = this.getNumberOfCollectionItemsIfAny(pageFormFields, fieldIdToSubstitute);
                                    numberCollectionItemsAsPlaceholder = this.getNewNumberOfCollectionItemsIfHigher(newNumberOfCollectionItemsAsPlaceholder, numberCollectionItemsAsPlaceholder);
                                }
                                stringToResolve = this.substitute(pageFormFields, stringToResolve, startSubstitutionIndex, fieldIdToSubstitute, colItemIndex);
                                scanIndex = this.resetScanIndexAfterSubstitution(startSubstitutionIndex, pageFormFields, fieldIdToSubstitute, colItemIndex);
                            }
                            isCollecting = false;
                            fieldIdToSubstitute = '';
                        }
                        else if (!this.isOpeningPlaceholder(stringToResolve, scanIndex)) {
                            fieldIdToSubstitute += stringToResolve.charAt(scanIndex);
                        }
                    }
                    scanIndex++;
                }
                if (colItemIndex < numberCollectionItemsAsPlaceholder - 1) {
                    stringToResolve += PlaceholderService_1.NEW_LINE;
                    stringToResolve += originalStringToResolve;
                    colItemIndex += 1;
                }
            }
        }
        return stringToResolve;
    };
    PlaceholderService.prototype.getNewNumberOfCollectionItemsIfHigher = function (newNumberOfCollectionItemsAsPlaceholder, numberCollectionItemsAsPlaceholder) {
        return newNumberOfCollectionItemsAsPlaceholder > numberCollectionItemsAsPlaceholder ?
            newNumberOfCollectionItemsAsPlaceholder :
            numberCollectionItemsAsPlaceholder;
    };
    PlaceholderService.prototype.getNumberOfCollectionItemsIfAny = function (pageFormFields, fieldIdToSubstitute) {
        var fieldIds = fieldIdToSubstitute.split('.');
        var pageFormFieldsClone = this.fieldsUtils.cloneObject(pageFormFields);
        var numberCollectionItemsAsPlaceholder = 1;
        for (var index = 0; index < fieldIds.length; index++) {
            if (this.isNonEmptyCollection(pageFormFieldsClone)) {
                numberCollectionItemsAsPlaceholder = pageFormFieldsClone.length;
                break;
            }
            else if (pageFormFieldsClone[fieldIds[index]] === undefined) {
                break;
            }
            else {
                pageFormFieldsClone = pageFormFieldsClone[fieldIds[index]];
            }
        }
        return numberCollectionItemsAsPlaceholder;
    };
    PlaceholderService.prototype.isMatchingPlaceholderPattern = function (fieldIdToSubstitute) {
        return fieldIdToSubstitute.match(PlaceholderService_1.PLACEHOLDER_PATTERN);
    };
    PlaceholderService.prototype.isFieldIdInFormFields = function (fieldIdToSubstitute, pageFormFields, collectionItemIndex) {
        var fieldValue = this.getFieldValue(pageFormFields, fieldIdToSubstitute, collectionItemIndex);
        return fieldValue ? this.isSimpleTypeOrCollectionOfSimpleTypes(fieldValue) : fieldValue !== undefined;
    };
    PlaceholderService.prototype.isSimpleTypeOrCollectionOfSimpleTypes = function (fieldValue) {
        return !this.isObject(fieldValue) && (this.isArray(fieldValue) ? this.isSimpleArray(fieldValue) : true);
    };
    PlaceholderService.prototype.isSimpleArray = function (fieldValue) {
        return !this.isObject(fieldValue[0]) && !Array.isArray(fieldValue[0]) && fieldValue[0] !== undefined;
    };
    PlaceholderService.prototype.isStartingPlaceholder = function (stringToResolve, scanIndex) {
        return stringToResolve.charAt(scanIndex) === PlaceholderService_1.STARTING_PLACEHOLDER;
    };
    PlaceholderService.prototype.isStartPlaceholderAndNotCollecting = function (stringToResolve, scanIndex, isCollectingPlaceholder) {
        return this.isStartingPlaceholder(stringToResolve, scanIndex) && !isCollectingPlaceholder;
    };
    PlaceholderService.prototype.isClosingPlaceholder = function (stringToResolve, scanIndex) {
        return stringToResolve.charAt(scanIndex) === PlaceholderService_1.CLOSING_PLACEHOLDER;
    };
    PlaceholderService.prototype.isOpeningPlaceholder = function (stringToResolve, scanIndex) {
        return stringToResolve.charAt(scanIndex) === PlaceholderService_1.OPENING_PLACEHOLDER;
    };
    PlaceholderService.prototype.substitute = function (pageFormFields, stringToResolve, startSubstitutionIndex, fieldIdToSubstitute, collectionItemIndex) {
        var replacedString = stringToResolve.substring(startSubstitutionIndex)
            .replace('${'.concat(fieldIdToSubstitute).concat('}'), this.getSubstitutionValueOrEmpty(pageFormFields, fieldIdToSubstitute, collectionItemIndex));
        return stringToResolve.substring(0, startSubstitutionIndex).concat(replacedString);
    };
    PlaceholderService.prototype.resetScanIndexAfterSubstitution = function (startSubstitutionIndex, pageFormFields, fieldIdToSubstitute, collectionItemIndex) {
        return startSubstitutionIndex + this.getSubstitutionValueLengthOrZero(pageFormFields, fieldIdToSubstitute, collectionItemIndex);
    };
    PlaceholderService.prototype.getSubstitutionValueOrEmpty = function (pageFormFields, fieldIdToSubstitute, collectionItemIndex) {
        var fieldValue = this.getFieldValue(pageFormFields, fieldIdToSubstitute, collectionItemIndex);
        if (fieldValue instanceof Array) {
            fieldValue = fieldValue.join(', ');
        }
        return fieldValue ? fieldValue : '';
    };
    PlaceholderService.prototype.getFieldValue = function (pageFormFields, fieldIdToSubstitute, collectionItemIndex) {
        var fieldIds = fieldIdToSubstitute.split('.');
        for (var index = 0; index < fieldIds.length; index++) {
            if (this.isNonEmptyCollection(pageFormFields) && this.isLeaf(fieldIds.length, index)) {
                var placeholderSuffix = fieldIds[fieldIds.length - 1];
                pageFormFields = pageFormFields[collectionItemIndex]['value'][placeholderSuffix];
                return pageFormFields;
            }
            else {
                if (pageFormFields !== undefined && this.isNonEmptyArray(pageFormFields[fieldIds[index]])
                    && !this.isCollection(pageFormFields[fieldIds[index]])) {
                    pageFormFields = pageFormFields[fieldIds[index] + FieldsUtils.LABEL_SUFFIX];
                }
                else if (this.isNonEmptyCollection(pageFormFields)) {
                    pageFormFields = pageFormFields[collectionItemIndex]['value'][fieldIds[index]];
                }
                else if (pageFormFields !== undefined && pageFormFields[fieldIds[index]] !== undefined) {
                    pageFormFields = pageFormFields[fieldIds[index]];
                }
                else {
                    return undefined;
                }
            }
        }
        if (this.isNonEmptyArray(pageFormFields) && this.isCollection(pageFormFields)) {
            pageFormFields = pageFormFields.map(function (fieldValue) { return fieldValue['value']; });
        }
        return pageFormFields;
    };
    PlaceholderService.prototype.isLeaf = function (length, currentIndex) {
        return length === currentIndex + 1;
    };
    PlaceholderService.prototype.isNonEmptyCollection = function (pageFormFields) {
        return this.isNonEmptyArray(pageFormFields) && this.isCollection(pageFormFields);
    };
    PlaceholderService.prototype.isNonEmptyArray = function (pageFormFields) {
        return Array.isArray(pageFormFields) && pageFormFields[0];
    };
    PlaceholderService.prototype.isCollection = function (pageFormFields) {
        return pageFormFields[0]['value'];
    };
    PlaceholderService.prototype.getSubstitutionValueLengthOrZero = function (pageFormFields, fieldIdToSubstitute, collectionItemIndex) {
        return pageFormFields[fieldIdToSubstitute] ? this.getSubstitutionValueOrEmpty(pageFormFields, fieldIdToSubstitute, collectionItemIndex)
            .toString().length : 0;
    };
    PlaceholderService.prototype.getType = function (elem) {
        return Object.prototype.toString.call(elem).slice(8, -1);
    };
    PlaceholderService.prototype.isObject = function (elem) {
        return this.getType(elem) === 'Object';
    };
    ;
    PlaceholderService.prototype.isArray = function (elem) {
        return this.getType(elem) === 'Array';
    };
    ;
    var PlaceholderService_1;
    PlaceholderService.PLACEHOLDER_PATTERN = /^[a-zA-Z0-9_.\]\[]+$/;
    PlaceholderService.STARTING_PLACEHOLDER = '$';
    PlaceholderService.OPENING_PLACEHOLDER = '{';
    PlaceholderService.CLOSING_PLACEHOLDER = '}';
    PlaceholderService.NEW_LINE = "\n___\n";
    PlaceholderService = PlaceholderService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [FieldsUtils])
    ], PlaceholderService);
    return PlaceholderService;
}());
export { PlaceholderService };
//# sourceMappingURL=placeholder.service.js.map