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';
// @dynamic
var OrderService = /** @class */ (function () {
    function OrderService() {
        /**
         * @deprecated Use `sort` function instead or `compareAsc`
         * @type {(a:Orderable, b:Orderable)=>number}
         */
        this.sortAsc = OrderService_1.DEFAULT_COMPARE_FUNCTION;
    }
    OrderService_1 = OrderService;
    /**
     * Clone and sort array. Ascending order used by default.
     *
     * @param array Array to sort
     * @returns {Orderable[]} Sorted clone array.
     */
    OrderService.prototype.sort = function (array, sortingFunction) {
        if (sortingFunction === void 0) { sortingFunction = this.sortAsc; }
        return array
            .slice()
            .sort(sortingFunction);
    };
    OrderService.prototype.deepSort = function (case_fields) {
        var _this = this;
        case_fields.forEach(function (caseField) {
            if (_this.isComplex(caseField)) {
                var sorted = _this.deepSort(caseField.field_type.complex_fields);
                caseField.field_type.complex_fields = sorted;
            }
            if (_this.isCollection(caseField)) {
                var sorted = _this.deepSort(caseField.field_type.collection_field_type.complex_fields);
                caseField.field_type.collection_field_type.complex_fields = sorted;
            }
            if (caseField.hidden !== true && !caseField.order) {
                if (_this.isComplex(caseField)) {
                    _this.deriveOrderFromChildComplexFields(caseField, caseField.field_type);
                }
                if (_this.isCollection(caseField)) {
                    _this.deriveOrderFromChildComplexFields(caseField, caseField.field_type.collection_field_type);
                }
            }
            if (_this.isOneOfFixedListTypes(caseField)) {
                caseField.field_type.fixed_list_items = _this.sort(caseField.field_type.fixed_list_items);
            }
        });
        return this.sort(case_fields);
    };
    OrderService.prototype.isCollection = function (caseField) {
        return caseField.field_type.type === 'Collection';
    };
    OrderService.prototype.isComplex = function (caseField) {
        return caseField.field_type.type === 'Complex';
    };
    OrderService.prototype.isOneOfFixedListTypes = function (caseField) {
        return caseField.field_type.type === 'FixedList' ||
            caseField.field_type.type === 'MultiSelectList' ||
            caseField.field_type.type === 'FixedRadioList';
    };
    OrderService.prototype.deriveOrderFromChildComplexFields = function (caseField, fieldType) {
        if (fieldType && fieldType.complex_fields && fieldType.complex_fields.length > 0) {
            var caseFieldOptional = fieldType.complex_fields.find(function (e) { return e.order !== undefined; });
            if (caseFieldOptional !== undefined) {
                caseField.order = caseFieldOptional.order;
            }
        }
    };
    var OrderService_1;
    OrderService.DEFAULT_COMPARE_FUNCTION = function (a, b) {
        var aOrdered = a.order === 0 || a.order;
        var bOrdered = b.order === 0 || b.order;
        if (!aOrdered) {
            return !bOrdered ? 0 : 1;
        }
        if (!bOrdered) {
            return -1;
        }
        return a.order - b.order;
    };
    OrderService = OrderService_1 = __decorate([
        Injectable()
    ], OrderService);
    return OrderService;
}());
export { OrderService };
//# sourceMappingURL=order.service.js.map