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 { CaseField } from '../../domain/definition';
import { CurrencyPipe, } from '@angular/common';
import { DatePipe } from '../../components/palette/utils';
// @dynamic
var FieldsUtils = /** @class */ (function () {
    function FieldsUtils() {
    }
    FieldsUtils_1 = FieldsUtils;
    FieldsUtils.toValuesMap = function (caseFields) {
        var valueMap = {};
        caseFields.forEach(function (field) {
            valueMap[field.id] = FieldsUtils_1.prepareValue(field);
        });
        return valueMap;
    };
    FieldsUtils.prepareValue = function (field) {
        if (field.value) {
            return field.value;
        }
        else if (field.field_type.type === 'Complex') {
            var valueMap_1 = {};
            field.field_type.complex_fields.forEach(function (complexField) {
                valueMap_1[complexField.id] = FieldsUtils_1.prepareValue(complexField);
            });
            return valueMap_1;
        }
    };
    FieldsUtils.getMoneyGBP = function (fieldValue) {
        return fieldValue ? FieldsUtils_1.currencyPipe.transform(fieldValue / 100, 'GBP', 'symbol') : fieldValue;
    };
    FieldsUtils.getDate = function (fieldValue) {
        try {
            return FieldsUtils_1.datePipe.transform(fieldValue, null, 'dd-MM-yyyy');
        }
        catch (e) {
            return this.textForInvalidField('Date', fieldValue);
        }
    };
    FieldsUtils.getFixedListLabelByCodeOrEmpty = function (field, code) {
        var relevantItem = code ? field.field_type.fixed_list_items.find(function (item) { return item.code === code; }) : '';
        return relevantItem ? relevantItem.label : '';
    };
    FieldsUtils.textForInvalidField = function (type, invalidValue) {
        return "{ Invalid " + type + ": " + invalidValue + " }";
    };
    FieldsUtils.getType = function (elem) {
        return Object.prototype.toString.call(elem).slice(8, -1);
    };
    FieldsUtils.isObject = function (elem) {
        return this.getType(elem) === 'Object';
    };
    ;
    FieldsUtils.isNonEmptyObject = function (elem) {
        return this.isObject(elem) && Object.keys(elem).length !== 0;
    };
    ;
    FieldsUtils.areCollectionValuesSimpleFields = function (fieldValue) {
        return !this.isObject(fieldValue[0]['value']) && !Array.isArray(fieldValue[0]['value']) && fieldValue[0]['value'] !== undefined;
    };
    FieldsUtils.isCollectionOfSimpleTypes = function (fieldValue) {
        return this.isCollection(fieldValue) && this.areCollectionValuesSimpleFields(fieldValue);
    };
    FieldsUtils.isMultiSelectValue = function (form) {
        return this.isNonEmptyArray(form) && !this.isCollectionWithValue(form);
    };
    FieldsUtils.isNonEmptyArray = function (pageFormFields) {
        return Array.isArray(pageFormFields) && pageFormFields[0] !== undefined;
    };
    FieldsUtils.isCollectionWithValue = function (pageFormFields) {
        return pageFormFields[0]['value'] !== undefined;
    };
    FieldsUtils.isCollection = function (pageFormFields) {
        return this.isNonEmptyArray(pageFormFields) && this.isCollectionWithValue(pageFormFields);
    };
    FieldsUtils.cloneObject = function (obj) {
        return Object.assign({}, obj);
    };
    FieldsUtils.isEmpty = function (value) {
        return value === null || value === undefined;
    };
    // temporary function until this can be moved to CaseView class (RDM-2681)
    FieldsUtils.getCaseFields = function (caseView) {
        var caseDataFields = caseView.tabs.reduce(function (acc, tab) {
            return acc.concat(tab.fields);
        }, []);
        var metadataFields = caseView.metadataFields;
        return metadataFields.concat(caseDataFields.filter(function (caseField) {
            return metadataFields.findIndex(function (metadataField) { return metadataField.id === caseField.id; }) < 0;
        }));
    };
    FieldsUtils.prototype.buildCanShowPredicate = function (eventTrigger, form) {
        var currentState = this.getCurrentEventState(eventTrigger, form);
        return function (page) {
            return page.parsedShowCondition.match(currentState);
        };
    };
    FieldsUtils.prototype.getCurrentEventState = function (eventTrigger, form) {
        return this.mergeCaseFieldsAndFormFields(eventTrigger.case_fields, form.controls['data'].value);
    };
    FieldsUtils.prototype.cloneObject = function (obj) {
        return Object.assign({}, obj);
    };
    FieldsUtils.prototype.cloneCaseField = function (obj) {
        return Object.assign(new CaseField(), obj);
    };
    FieldsUtils.prototype.mergeCaseFieldsAndFormFields = function (caseFields, formFields) {
        return this.mergeFields(caseFields, formFields, FieldsUtils_1.DEFAULT_MERGE_FUNCTION);
    };
    FieldsUtils.prototype.mergeLabelCaseFieldsAndFormFields = function (caseFields, formFields) {
        return this.mergeFields(caseFields, formFields, FieldsUtils_1.LABEL_MERGE_FUNCTION);
    };
    FieldsUtils.prototype.mergeFields = function (caseFields, formFields, mergeFunction) {
        var _this = this;
        var result = this.cloneObject(formFields);
        caseFields.forEach(function (field) {
            mergeFunction(field, result);
            if (field.field_type && field.field_type.complex_fields && field.field_type.complex_fields.length > 0) {
                result[field.id] = _this.mergeFields(field.field_type.complex_fields, result[field.id], mergeFunction);
            }
        });
        return result;
    };
    var FieldsUtils_1;
    FieldsUtils.currencyPipe = new CurrencyPipe('en-GB');
    FieldsUtils.datePipe = new DatePipe();
    FieldsUtils.LABEL_SUFFIX = '-LABEL';
    FieldsUtils.DEFAULT_MERGE_FUNCTION = function mergeFunction(field, result) {
        if (!result.hasOwnProperty(field.id)) {
            result[field.id] = field.value;
        }
    };
    FieldsUtils.LABEL_MERGE_FUNCTION = function mergeFunction(field, result) {
        if (!result.hasOwnProperty(field.id)) {
            result[field.id] = field.value;
        }
        switch (field.field_type.type) {
            case 'FixedList': {
                result[field.id] = FieldsUtils_1.getFixedListLabelByCodeOrEmpty(field, result[field.id] || field.value);
                break;
            }
            case 'MultiSelectList': {
                var fieldValue = result[field.id] || [];
                result[field.id + FieldsUtils_1.LABEL_SUFFIX] = [];
                fieldValue.forEach(function (code, idx) {
                    result[field.id + FieldsUtils_1.LABEL_SUFFIX][idx] = FieldsUtils_1.getFixedListLabelByCodeOrEmpty(field, code);
                });
                break;
            }
            case 'MoneyGBP': {
                var fieldValue = (result[field.id] || field.value);
                result[field.id] = FieldsUtils_1.getMoneyGBP(fieldValue);
                break;
            }
            case 'Date': {
                var fieldValue = (result[field.id] || field.value);
                result[field.id] = FieldsUtils_1.getDate(fieldValue);
                break;
            }
            case 'Collection': {
                var elements = (result[field.id] || field.value);
                if (elements) {
                    elements.forEach(function (elem) {
                        switch (field.field_type.collection_field_type.type) {
                            case 'MoneyGBP': {
                                elem.value = FieldsUtils_1.getMoneyGBP(elem.value);
                                break;
                            }
                            case 'Date': {
                                elem.value = FieldsUtils_1.getDate(elem.value);
                                break;
                            }
                        }
                    });
                }
                break;
            }
        }
    };
    FieldsUtils = FieldsUtils_1 = __decorate([
        Injectable()
    ], FieldsUtils);
    return FieldsUtils;
}());
export { FieldsUtils };
//# sourceMappingURL=fields.utils.js.map