"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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrdinalScaleModel = void 0;
const ScaleModel_1 = require("./ScaleModel");
const _ = __importStar(require("underscore"));
class OrdinalScaleModel extends ScaleModel_1.ScaleModel {
    defaults() {
        return Object.assign(Object.assign({}, ScaleModel_1.ScaleModel.prototype.defaults()), { _model_name: 'OrdinalScaleModel', _view_name: 'OrdinalScale', domain: [] });
    }
    typedRange(values) {
        return values;
    }
    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((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 (const 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=OrdinalScaleModel.js.map