﻿if (!Cltr) var Cltr = {};
if (!Cltr.A4) Cltr.A4 = {};
if (!Cltr.A4.Utils) Cltr.A4.Utils = {};

var DataTypeUtil;

(function () {
    DataTypeUtil = Cltr.A4.Utils.DataTypeUtil = {
        IsFunction: function (objectRef) {
            /// <summary>Determines if the supplied object is a function</summary>
            /// <param name="objectRef">Object to check</param>
            /// <returns>Boolean</returns>
            return typeof (objectRef) == 'function' && (!Function.prototype.call || typeof (objectRef.call) == 'function');
        },
        IsArrayNullOrEmpty: function (c) {
            if (this.IsFunction(c)){
                return (c() == null || c().length == 0);
            } else {
                return (c == null || c.length == 0);
            }
        },
        getPropertyVal: function(property){
            /// <summary>For use when you need the value of a property but are unsure whether it is a function</summary>
            /// <param name="objectRef">Property</param>
            /// <returns>Boolean</returns>
            return this.IsFunction(property) ? property() : property;
        },
        getArrayElementByProperty: function(propertyName, matchingValue, list){
            /// <summary>Retrieves an array element by matching on a specific property</summary>
            /// <param name="propertyName">String representation of the property to check</param>
            /// <param name="matchingValue">Value the 'propertyName' must match</param>
            /// <param name="list">Array to retrieve the element from</param>
            if (this.IsArrayNullOrEmpty(list)){ return null; }

            var realList = this.getPropertyVal(list);
            for (var o in realList){
                var currentItem = realList[o];
                if (this.getPropertyVal(currentItem[propertyName]) == matchingValue){
                    return currentItem;
                }
            }
            return null;
        }
    }
})();







