"use strict";
/* Copyright 2015 Bloomberg Finance L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const d3 = __importStar(require("d3"));
// var d3 =Object.assign({}, require("d3-array"), require("d3-scale"));
const is_typedarray_1 = __importDefault(require("is-typedarray"));
const _ = __importStar(require("underscore"));
// the following is a regex to match all valid time formats that can be
// generated with d3 as of 2nd March 2015. If new formats are added to d3
// those new formats need to be added to the regex
const time_format_regex = new RegExp("^(((((\\*)|(/*)|(-*))(\\s*)%([aAbBdeHIjmMLpSUwWyYZ]{1}))+)|((\\s*)%([cxX]{1})))$");
function deepCopy(obj) {
    // This makes a deep copy of JSON-parsable objects
    // (no cycling or recombining)
    // Backbone model attributes must be JSON parsable. Hence there is
    // no need for a fancier logic, and it is surprisingly efficient.
    if (is_typedarray_1.default(obj))
        return Object.assign(obj.slice(), obj);
    else if (_.isArray(obj)) {
        return obj.map(x => deepCopy(x));
    }
    else if (_.isNumber(obj) || _.isString(obj)) {
        return obj;
    }
    else if (_.isDate(obj)) {
        return new Date(obj.getTime());
    }
    else {
        const newobj = {};
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                newobj[key] = deepCopy(obj[key]);
            }
        }
        return newobj;
    }
}
exports.deepCopy = deepCopy;
function convert_dates(value) {
    // check if the array of value contains Date objects, if so
    // dates are serialized using the timestamp only, some parts of the code
    // use the Data objects, detect that and convert.
    // also, we set the .type property of TypedArray, to keep track of it being a
    // date array
    if (is_typedarray_1.default(value))
        return value;
    const convert_to_date = function (x) {
        const ar = new Float64Array(x.map(Number));
        ar.type = 'date';
        return ar;
    };
    if (value[0] instanceof Array && value[0][0] instanceof Date)
        value = value.map(convert_to_date);
    else if (value[0] instanceof Date)
        value = convert_to_date(value);
    return value;
}
exports.convert_dates = convert_dates;
function getCustomRange(array) {
    const first = array[0];
    const end = array[array.length - 1];
    let pivot;
    if (array[0] > array[1]) {
        pivot = d3.min(array);
    }
    else {
        pivot = d3.max(array);
    }
    return [d3.scaleLinear().range([first, pivot]), d3.scaleLinear().range([pivot, end])];
}
exports.getCustomRange = getCustomRange;
function is_valid_time_format(format) {
    return time_format_regex.test(format);
}
exports.is_valid_time_format = is_valid_time_format;
function is_array(x) {
    return (x instanceof Array) || is_typedarray_1.default(x);
}
exports.is_array = is_array;
function getDate(elem) {
    if (elem === undefined || elem === null) {
        return null;
    }
    return new Date(elem);
}
exports.getDate = getDate;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7O0dBYUc7Ozs7Ozs7Ozs7OztBQUVILHVDQUF5QjtBQUN6Qix1RUFBdUU7QUFDdkUsa0VBQXlDO0FBQ3pDLDhDQUFnQztBQUVoQyx1RUFBdUU7QUFDdkUseUVBQXlFO0FBQ3pFLGtEQUFrRDtBQUNsRCxNQUFNLGlCQUFpQixHQUFHLElBQUksTUFBTSxDQUFDLGtGQUFrRixDQUFDLENBQUM7QUFFekgsU0FBZ0IsUUFBUSxDQUFDLEdBQUc7SUFDeEIsa0RBQWtEO0lBQ2xEL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