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 { FieldTypeSanitiser } from './field-type-sanitiser';
import { FieldsUtils } from '../fields';
var FormValueService = /** @class */ (function () {
    function FormValueService(fieldTypeSanitiser) {
        this.fieldTypeSanitiser = fieldTypeSanitiser;
    }
    FormValueService_1 = FormValueService;
    /**
   * Gets value of a field based on fieldKey which is a dot separated reference to value and collection index.
   * There are two exeptions:
   * 1) In case of a multiselect being identified as a leaf a '-LABEL' suffix is appended to the key and values og that key are returned
   *      form= { 'list': ['code1', 'code2'],
   *              'list-LABEL': ['label1', 'label2'] },
   *      fieldKey=list,
   *      colIndex=0,
   *      value=label1, label2
   * 2) In case of a collection of simple fields is identified as a leaf all values are joined seperated by a comma
   *      form= { 'collection': [{ 'value': 'value1' }, { 'value': 'value2' }] }
   *      fieldKey=collection
   *      colIndex=1
   *      value=value1, value2
   *
   * Other examples:
   * 1) simple field reference: form={ 'PersonFirstName': 'John' }, fieldKey=PersonFirstName, value=John
   * 2) complex field reference:
   *      form= { complex1': {
                    'simple11': 'value11',
                    'simple12': 'value12',
                    'complex2': {
                      'simple21': 'value21'
                    }
                }},
   *      fieldKey=complex1.complex2.simple21
   *      colIndex=0,
   *      value=value21
   * 3) complex field with collection field with complex field reference:
   *      form= { 'complex1': {
   *               'collection1': [
   *               { 'value': {
   *                   'complex2': {
   *                     'simple1': 'value1',
   *                     'complex3': {
   *                       'complex4': {
   *                         'simple2': 'value12'
   *                       }
   *                     }
   *                   }
   *                 }
   *               },
   *               { 'value': {
   *                   'complex2': {
   *                     'simple1': 'value2',
   *                     'complex3': {
   *                       'complex4': {
   *                         'simple2': 'value21'
   *                       }
   *                     }
   *                   }
   *                 }
   *               },
   *               { 'value': {
   *                   'complex2': {
   *                     'simple1': 'value3',
   *                     'complex3': {
   *                       'complex4': {
   *                         'simple2': 'value31'
   *                       }
   *                     }
   *                   }
   *                 }
   *               }
   *             ]}}
   *      fieldKey=complex1.collection1.complex2.complex3.complex4.simple2
   *      colIndex=1,
   *      value=value21
   * 4) collection of complex types
   *      form= { 'collection1': [
   *               { 'value': {'complex1': {
   *                             'simple1': 'value11',
   *                             'complex2': {
   *                               'complex3': {
   *                                 'simple2': 'value12'
   *                               }
   *                             }
   *                         }}
   *               },
   *               { 'value': {'complex1': {
   *                             'simple1': 'value21',
   *                             'complex2': {
   *                               'complex3': {
   *                                 'simple2': 'value22'
   *                               }
   *                             }
   *                         }}
   *               },
   *               { 'value': {'complex1': {
   *                             'simple1': 'value31',
   *                             'complex2': {
   *                               'complex3': {
   *                                 'simple2': 'value32'
   *                               }
   *                             }
   *                           }}
   *               }
   *             ]}
   *      fieldKey=collection1.complex1.complex2.complex3.simple2
   *      colIndex=2
   *      value=value32
   *
   * If key is pointing at a complex or collection leaf (not simple, collection of simple or multiselect types) then undefined is returned.
   * Also no key referring a leaf that is contained within collection will contain index number. The index is passed as an argument to the
   * method.
   * @param form form
   * @param fieldKey dot separated reference to value
   * @param colIndex index of collection item being referenced or 0 otherwise
   * @returns {string} simple or combined value of a field
   **/
    FormValueService.getFieldValue = function (form, fieldKey, colIndex) {
        var fieldIds = fieldKey.split('.');
        var currentFieldId = fieldIds[0];
        var currentForm = form[currentFieldId];
        if (FieldsUtils.isMultiSelectValue(currentForm)) {
            return form[currentFieldId + FormValueService_1.LABEL_SUFFIX].join(', ');
        }
        else if (FieldsUtils.isCollectionOfSimpleTypes(currentForm)) {
            return currentForm.map(function (fieldValue) { return fieldValue['value']; }).join(', ');
        }
        else if (FieldsUtils.isCollection(currentForm)) {
            return this.getFieldValue(currentForm[colIndex]['value'], fieldIds.slice(1).join('.'), colIndex);
        }
        else if (FieldsUtils.isNonEmptyObject(currentForm)) {
            return this.getFieldValue(currentForm, fieldIds.slice(1).join('.'), colIndex);
        }
        else {
            return currentForm;
        }
    };
    FormValueService.prototype.sanitise = function (rawValue) {
        return this.sanitiseObject(rawValue);
    };
    FormValueService.prototype.sanitiseCaseReference = function (reference) {
        // strip non digits
        var s = reference.replace(/[\D]/g, '');
        if (s.length > 16) {
            return s.substr(s.length - 16, 16);
        }
        return s;
    };
    FormValueService.prototype.filterCurrentPageFields = function (caseFields, editFrom) {
        var cloneForm = JSON.parse(JSON.stringify(editFrom));
        Object.keys(cloneForm['data']).forEach(function (key) {
            if (caseFields.findIndex(function (element) { return element.id === key; }) < 0) {
                delete cloneForm['data'][key];
            }
        });
        return cloneForm;
    };
    FormValueService.prototype.sanitiseDynamicLists = function (caseFields, editForm) {
        return this.fieldTypeSanitiser.sanitiseLists(caseFields, editForm);
    };
    FormValueService.prototype.sanitiseObject = function (rawObject) {
        var _this = this;
        if (!rawObject) {
            return rawObject;
        }
        var sanitisedObject = {};
        Object.keys(rawObject).forEach(function (key) {
            if ('CaseReference' === key) {
                sanitisedObject[key] = _this.sanitiseValue(_this.sanitiseCaseReference(String(rawObject[key])));
            }
            else {
                sanitisedObject[key] = _this.sanitiseValue(rawObject[key]);
            }
        });
        return sanitisedObject;
    };
    FormValueService.prototype.sanitiseArray = function (rawArray) {
        var _this = this;
        if (!rawArray) {
            return rawArray;
        }
        rawArray.forEach(function (item) {
            if (item.hasOwnProperty('value')) {
                item.value = _this.sanitiseValue(item.value);
            }
        });
        return rawArray;
    };
    FormValueService.prototype.sanitiseValue = function (rawValue) {
        if (Array.isArray(rawValue)) {
            return this.sanitiseArray(rawValue);
        }
        switch (typeof rawValue) {
            case 'object':
                return this.sanitiseObject(rawValue);
            case 'string':
                return rawValue.trim();
            case 'number':
                return String(rawValue);
            default:
                return rawValue;
        }
    };
    var FormValueService_1;
    FormValueService.LABEL_SUFFIX = '-LABEL';
    FormValueService = FormValueService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [FieldTypeSanitiser])
    ], FormValueService);
    return FormValueService;
}());
export { FormValueService };
//# sourceMappingURL=form-value.service.js.map