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';
var FieldTypeSanitiser = /** @class */ (function () {
    function FieldTypeSanitiser() {
    }
    /**
     * This method finds dynamiclists in a form and replaces its string value, with
     * following example JSON format
     * @return {value: {code:'xyz',label:'XYZ'}, list_items: [{code:'xyz',label:'XYZ'},{code:'abc',label:'ABC'}]}
     * @param caseFields
     * @param editForm
     */
    FieldTypeSanitiser.prototype.sanitiseLists = function (caseFields, editForm) {
        var _this = this;
        this.getDynamicListsFromCaseFields(caseFields).forEach(function (dynamicField) {
            _this.getListOfKeysFromEditForm(editForm).forEach(function (key) {
                _this.createValueCodePairAlongWithListIfKeyExistsInForm(dynamicField, key, editForm);
            });
        });
    };
    FieldTypeSanitiser.prototype.createValueCodePairAlongWithListIfKeyExistsInForm = function (dynamicField, key, editForm) {
        if (dynamicField.id === key) {
            editForm['data'][key] = {
                value: this.getMatchingCodeFromListOfItems(dynamicField, editForm, key),
                list_items: dynamicField.list_items
            };
        }
    };
    FieldTypeSanitiser.prototype.getMatchingCodeFromListOfItems = function (dynamicField, editForm, key) {
        var result = dynamicField.list_items.filter(function (value) { return String(value.code) === editForm['data'][key]; });
        if (result.length > 0) {
            result[0].code = String(result[0].code);
            return result[0];
        }
        return {};
    };
    FieldTypeSanitiser.prototype.getListOfKeysFromEditForm = function (editForm) {
        return Object.keys(editForm['data']);
    };
    FieldTypeSanitiser.prototype.getDynamicListsFromCaseFields = function (caseFields) {
        return caseFields
            .filter(function (caseField) { return caseField.field_type.type === 'DynamicList'; });
    };
    FieldTypeSanitiser = __decorate([
        Injectable()
    ], FieldTypeSanitiser);
    return FieldTypeSanitiser;
}());
export { FieldTypeSanitiser };
//# sourceMappingURL=field-type-sanitiser.js.map