"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 obj.slice();
    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;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7O0dBYUc7Ozs7Ozs7Ozs7OztBQUVILHVDQUF5QjtBQUN6Qix1RUFBdUU7QUFDdkUsa0VBQXlDO0FBQ3pDLDhDQUFnQztBQUVoQyx1RUFBdUU7QUFDdkUseUVBQXlFO0FBQ3pFLGtEQUFrRDtBQUNsRCxNQUFNLGlCQUFpQixHQUFHLElBQUksTUFBTSxDQUFDLGtGQUFrRixDQUFDLENBQUM7QUFFekgsU0FBZ0IsUUFBUSxDQUFDLEdBQUc7SUFDeEIsa0RBQWtEO0lBQ2xELDhCQUE4QjtJQUM5QixrRUFBa0U7SUFDbEUsaUVBQWlFO0lBQ2pFLElBQUcsdUJBQVksQ0F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