"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BoxplotModel = void 0;
const d3 = __importStar(require("d3"));
const _ = __importStar(require("underscore"));
const MarkModel_1 = require("./MarkModel");
const serialize = __importStar(require("./serialize"));
class BoxplotModel extends MarkModel_1.MarkModel {
    defaults() {
        return Object.assign(Object.assign({}, MarkModel_1.MarkModel.prototype.defaults()), { _model_name: 'BoxplotModel', _view_name: 'Boxplot', x: [], y: [], scales_metadata: {
                x: { orientation: 'horizontal', dimension: 'x' },
                y: { orientation: 'vertical', dimension: 'y' },
            }, stroke: null, box_fill_color: 'dodgerblue', outlier_fill_color: 'gray', opacities: [], box_width: null, auto_detect_outliers: true });
    }
    initialize() {
        super.initialize.apply(this, arguments);
        this.on_some_change(['x', 'y'], this.update_data, this);
        this.on_some_change(['preserve_domain'], this.update_domains, this);
        this.update_data();
        this.update_domains();
    }
    get_data_dict(data, index) {
        return data.dataDict;
    }
    update_data() {
        let xData = this.get('x');
        let yData = this.get('y');
        yData.forEach((elm) => {
            elm.sort((a, b) => {
                return a - b;
            });
        });
        if (xData.length > yData.length) {
            xData = xData.slice(0, yData.length);
        }
        else if (xData.length < yData.length) {
            yData = yData.slice(0, xData.length);
        }
        this.mark_data = _.zip(xData, yData);
        this.update_domains();
        this.trigger('data_updated');
    }
    update_domains() {
        // color scale needs an issue in DateScaleModel to be fixed. It
        // should be moved here as soon as that is fixed.
        const scales = this.get('scales');
        if (!this.get('preserve_domain').x && this.mark_data) {
            scales.x.compute_and_set_domain(this.mark_data.map((elem) => {
                return elem[0];
            }), this.model_id + '_x');
        }
        else {
            scales.x.del_domain([], this.model_id + '_x');
        }
        if (!this.get('preserve_domain').y && this.mark_data) {
            //The values are sorted, so we are using that to calculate the min/max
            const min = d3.min(this.mark_data.map((d) => {
                return d[1][0];
            }));
            const max = d3.max(this.mark_data.map((d) => {
                const values = d[1];
                return values[values.length - 1];
            }));
            scales.y.set_domain([min, max], this.model_id + '_y');
        }
        else {
            scales.y.del_domain([], this.model_id + '_y');
        }
    }
}
exports.BoxplotModel = BoxplotModel;
BoxplotModel.serializers = Object.assign(Object.assign({}, MarkModel_1.MarkModel.serializers), { x: serialize.array_or_json, y: serialize.array_or_json });
//# sourceMappingURL=BoxplotModel.js.map