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;
};
import { Injectable } from '@angular/core';
import { FieldsUtils } from '../../../services/fields/fields.utils';
import { FormValueService } from '../../../services';
// @dynamic
var PlaceholderService = /** @class */ (function () {
    function PlaceholderService() {
    }
    PlaceholderService.prototype.resolvePlaceholders = function (pageFormFields, stringToResolve) {
        var ps = new PlaceholderSubstitutor({ pageFormFields: pageFormFields, stringToResolve: stringToResolve });
        return ps.resolvePlaceholders();
    };
    PlaceholderService = __decorate([
        Injectable()
    ], PlaceholderService);
    return PlaceholderService;
}());
export { PlaceholderService };
var PlaceholderSubstitutor = /** @class */ (function () {
    function PlaceholderSubstitutor(values) {
        this.resolvedFormValues = [];
        this.stringToResolve = values.stringToResolve;
        this.originalStringToResolve = values.stringToResolve;
        this.pageFormFields = values.pageFormFields;
    }
    PlaceholderSubstitutor.prototype.resolvePlaceholders = function () {
        while (this.hasUnresolvedPlaceholder()) {
            this.resetPlaceholderSubstitutor();
            while (this.doesPlaceholderContainCollectionItems()) {
                while (this.isScanningStringToResolve()) {
                    if (this.isStartPlaceholderAndNotCollecting()) {
                        this.setStartCollecting();
                    }
                    else if (this.isCollecting) {
                        if (this.isClosingPlaceholder()) {
                            this.substitute();
                        }
                        else if (!this.isOpeningPlaceholder()) {
                            this.appendCharacter();
                        }
                    }
                    this.scanIndex++;
                }
                this.appendOriginalStringIfCollectionItemAsPlaceholder();
            }
        }
        return this.stringToResolve;
    };
    PlaceholderSubstitutor.prototype.isScanningStringToResolve = function () {
        return this.scanIndex < this.stringToResolve.length;
    };
    PlaceholderSubstitutor.prototype.doesPlaceholderContainCollectionItems = function () {
        return this.numberCollectionItemsAsPlaceholder-- > 0;
    };
    PlaceholderSubstitutor.prototype.hasUnresolvedPlaceholder = function () {
        return this.stringToResolve
            && typeof this.stringToResolve === 'string'
            && this.stringToResolve.match(PlaceholderSubstitutor.PLACEHOLDER_PATTERN);
    };
    PlaceholderSubstitutor.prototype.isStartPlaceholderAndNotCollecting = function () {
        return this.isStartingPlaceholder() && !this.isCollecting;
    };
    PlaceholderSubstitutor.prototype.isOpeningPlaceholder = function () {
        return this.stringToResolve.charAt(this.scanIndex) === PlaceholderSubstitutor.OPENING_PLACEHOLDER;
    };
    PlaceholderSubstitutor.prototype.isClosingPlaceholder = function () {
        return this.stringToResolve.charAt(this.scanIndex) === PlaceholderSubstitutor.CLOSING_PLACEHOLDER;
    };
    PlaceholderSubstitutor.prototype.resetPlaceholderSubstitutor = function () {
        this.scanIndex = 0;
        this.numberCollectionItemsAsPlaceholder = 1;
        this.collectionItemIndex = 0;
        this.fieldIdToSubstitute = '';
        this.startSubstitutionIndex = -1;
        this.isCollecting = false;
        this.resolvedFormValues[this.collectionItemIndex] = {};
    };
    PlaceholderSubstitutor.prototype.substitute = function () {
        if (this.isMatchingPlaceholderPattern() && this.isFieldIdInFormFields()) {
            this.updateNumberOfCollectionItemsAsPlaceholder();
            if (this.isFieldIdToSubstituteReferringItself()) {
                this.substituteWithEmptyString();
            }
            else {
                this.substituteFromFormFields();
            }
        }
        else {
            this.substituteWithEmptyString();
        }
        this.isCollecting = false;
        this.fieldIdToSubstitute = '';
    };
    PlaceholderSubstitutor.prototype.appendOriginalStringIfCollectionItemAsPlaceholder = function () {
        if (this.collectionItemIndex < this.numberCollectionItemsAsPlaceholder - 1) {
            this.stringToResolve += PlaceholderSubstitutor.NEW_LINE + this.originalStringToResolve;
            this.collectionItemIndex += 1;
            this.resolvedFormValues[this.collectionItemIndex] = {};
        }
    };
    PlaceholderSubstitutor.prototype.setStartCollecting = function () {
        this.isCollecting = true;
        this.startSubstitutionIndex = this.scanIndex;
    };
    PlaceholderSubstitutor.prototype.appendCharacter = function () {
        this.fieldIdToSubstitute += this.stringToResolve.charAt(this.scanIndex);
    };
    PlaceholderSubstitutor.prototype.isMatchingPlaceholderPattern = function () {
        return this.fieldIdToSubstitute.match(PlaceholderSubstitutor.PLACEHOLDER_CONTENT_PATTERN);
    };
    PlaceholderSubstitutor.prototype.isFieldIdInFormFields = function () {
        return this.getFieldValue() !== undefined;
    };
    PlaceholderSubstitutor.prototype.isFieldIdToSubstituteReferringItself = function () {
        var value = this.getSubstitutionValueOrEmpty();
        return '${'.concat(this.fieldIdToSubstitute).concat('}') === value;
    };
    PlaceholderSubstitutor.prototype.getSubstitutionValueLengthOrZero = function () {
        return this.pageFormFields[this.fieldIdToSubstitute] ? this.getSubstitutionValueOrEmpty().toString().length : 0;
    };
    PlaceholderSubstitutor.prototype.getFieldValue = function () {
        if (this.resolvedFormValues[this.collectionItemIndex][this.fieldIdToSubstitute]) {
            return this.resolvedFormValues[this.collectionItemIndex][this.fieldIdToSubstitute];
        }
        else {
            var fieldValue = FormValueService.getFieldValue(this.pageFormFields, this.fieldIdToSubstitute, this.collectionItemIndex);
            this.resolvedFormValues[this.collectionItemIndex][this.fieldIdToSubstitute] = fieldValue;
            return this.resolvedFormValues[this.collectionItemIndex][this.fieldIdToSubstitute];
        }
    };
    PlaceholderSubstitutor.prototype.getSubstitutionValueOrEmpty = function () {
        var fieldValue = this.getFieldValue();
        return FieldsUtils.isEmpty(fieldValue) ? '' : fieldValue;
    };
    PlaceholderSubstitutor.prototype.getNumberOfCollectionItemsIfAny = function () {
        var fieldIds = this.fieldIdToSubstitute.split('.');
        var pageFormFieldsClone = FieldsUtils.cloneObject(this.pageFormFields);
        var numberCollectionItemsAsPlaceholder = 1;
        for (var index = 0; index < fieldIds.length; index++) {
            if (FieldsUtils.isCollection(pageFormFieldsClone)) {
                numberCollectionItemsAsPlaceholder = pageFormFieldsClone.length;
                break;
            }
            else if (pageFormFieldsClone[fieldIds[index]] === undefined) {
                break;
            }
            else {
                pageFormFieldsClone = pageFormFieldsClone[fieldIds[index]];
            }
        }
        return numberCollectionItemsAsPlaceholder;
    };
    PlaceholderSubstitutor.prototype.getNewNumberOfCollectionItemsIfHigher = function (newNumberOfCollectionItemsAsPlaceholder, numberCollectionItemsAsPlaceholder) {
        return newNumberOfCollectionItemsAsPlaceholder > numberCollectionItemsAsPlaceholder ?
            newNumberOfCollectionItemsAsPlaceholder :
            numberCollectionItemsAsPlaceholder;
    };
    PlaceholderSubstitutor.prototype.isStartingPlaceholder = function () {
        return this.stringToResolve.charAt(this.scanIndex) === PlaceholderSubstitutor.STARTING_PLACEHOLDER;
    };
    PlaceholderSubstitutor.prototype.updateNumberOfCollectionItemsAsPlaceholder = function () {
        if (this.fieldIdToSubstitute.split('.').length > 1) {
            var newNumberOfCollectionItemsAsPlaceholder = this.getNumberOfCollectionItemsIfAny();
            this.numberCollectionItemsAsPlaceholder =
                this.getNewNumberOfCollectionItemsIfHigher(newNumberOfCollectionItemsAsPlaceholder, this.numberCollectionItemsAsPlaceholder);
        }
    };
    PlaceholderSubstitutor.prototype.substituteFromFormFields = function () {
        var replacedString = this.stringToResolve.substring(this.startSubstitutionIndex)
            .replace('${'.concat(this.fieldIdToSubstitute).concat('}'), this.getSubstitutionValueOrEmpty());
        this.stringToResolve = this.stringToResolve.substring(0, this.startSubstitutionIndex).concat(replacedString);
        this.resetScanIndexAfterSubstitution();
    };
    PlaceholderSubstitutor.prototype.substituteWithEmptyString = function () {
        var replacedString = this.stringToResolve.substring(this.startSubstitutionIndex)
            .replace('${'.concat(this.fieldIdToSubstitute).concat('}'), '');
        this.stringToResolve = this.stringToResolve.substring(0, this.startSubstitutionIndex).concat(replacedString);
        this.scanIndex = this.startSubstitutionIndex;
    };
    PlaceholderSubstitutor.prototype.resetScanIndexAfterSubstitution = function () {
        this.scanIndex = this.startSubstitutionIndex + this.getSubstitutionValueLengthOrZero();
    };
    PlaceholderSubstitutor.PLACEHOLDER_CONTENT_PATTERN = /^[a-zA-Z0-9_.\]\[]+$/;
    PlaceholderSubstitutor.PLACEHOLDER_PATTERN = /\$\{[a-zA-Z0-9_.\]\[]+\}/;
    PlaceholderSubstitutor.STARTING_PLACEHOLDER = '$';
    PlaceholderSubstitutor.CLOSING_PLACEHOLDER = '}';
    PlaceholderSubstitutor.OPENING_PLACEHOLDER = '{';
    PlaceholderSubstitutor.NEW_LINE = "\n___\n";
    return PlaceholderSubstitutor;
}());
;
//# sourceMappingURL=placeholder.service.js.map