"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const ScaleModel_1 = require("./ScaleModel");
const _ = __importStar(require("underscore"));
class OrdinalScaleModel extends ScaleModel_1.ScaleModel {
    defaults() {
        return Object.assign({}, ScaleModel_1.ScaleModel.prototype.defaults(), { _model_name: "OrdinalScaleModel", _view_name: "OrdinalScale", domain: [] });
    }
    initialize(attributes, options) {
        super.initialize(attributes, options);
    }
    set_init_state() {
        this.type = "ordinal";
        this.min_from_data = true;
        this.max_from_data = true;
    }
    set_listeners() {
        this.on("change:domain", this.domain_changed, this);
        this.domain_changed();
        this.on("change:reverse", this.reverse_changed, this);
        this.reverse_changed();
    }
    domain_changed() {
        this.ord_domain = this.get("domain");
        if (this.ord_domain !== null && this.ord_domain.length !== 0) {
            this.max_from_data = false;
            this.min_from_data = false;
            this.domain = this.ord_domain.map(function (d) { return d; });
            this.trigger("domain_changed");
        }
        else {
            this.max_from_data = true;
            this.min_from_data = true;
            this.domain = [];
            this.update_domain();
        }
    }
    reverse_changed(model) {
        const prev_reverse = (model === undefined) ? false : model.previous("reverse");
        this.reverse = this.get("reverse");
        // the domain should be reversed only if the previous value of reverse
        // is different from the current value. During init, domain should be
        // reversed only if reverse is set to True.
        const reverse_domain = (prev_reverse + this.reverse) % 2;
        if (this.domain.length > 0 && reverse_domain === 1) {
            this.domain.reverse();
            this.trigger("domain_changed", this.domain);
        }
    }
    update_domain() {
        let domain = [];
        // TODO: check for hasOwnProperty
        for (let id in this.domains) {
            domain = _.union(domain, this.domains[id]);
        }
        if (this.domain.length !== domain.length ||
            (_.intersection(this.domain, domain)).length !== domain.length) {
            this.domain = domain;
            this.trigger("domain_changed", domain);
        }
    }
    compute_and_set_domain(data_array, id) {
        // Takes an array and calculates the domain for the particular
        // view. If you have the domain already calculated on your side,
        // call set_domain function.
        if (!this.min_from_data && !this.max_from_data) {
            return;
        }
        if (data_array.length === 0) {
            this.set_domain([], id);
            return;
        }
        const domain = _.flatten(data_array);
        if (this.get("reverse")) {
            domain.reverse();
        }
        this.set_domain(domain, id);
    }
}
exports.OrdinalScaleModel = OrdinalScaleModel;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiT3JkaW5hbFNjYWxlTW9kZWwuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvT3JkaW5hbFNjYWxlTW9kZWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7O0dBYUc7Ozs7Ozs7OztBQUVILDZDQUEwQztBQUMxQyw4Q0FBZ0M7QUFFaEMsTUFBYSxpQkFBa0IsU0FBUSx1QkFBVTtJQUU3QyxRQUFRO1FBQ0oseUJBQVcsdUJBQVUsQ0FBQyxTQUFTLENBQUMsUUFBUSxFQUFFLElBQ3RDLFdBQVcsRUFBRSxtQkFBbUIsRUFDaEMsVUFBVSxFQUFFLGNBQWMsRUFDMUIsTUFBTSxFQUFFLEVBQUUsSUFDWjtJQUNOLENBQUM7SUFFRCxVQUFVLENBQUMsVUFBVSxFQUFFLE9BQU87UUFDMUIsS0FBSyxDQUFDLFVBQVUsQ0FBQyxVQUFVLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDMUMsQ0FBQztJQUVELGNBQWM7UUFDVixJQUFJL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