"use strict";
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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const dummy_manager_1 = require("./dummy-manager");
const bqplot = __importStar(require("../index"));
const widget_utils_1 = require("./widget-utils");
describe('scatter mega >', () => {
    beforeEach(function () {
        return __awaiter(this, void 0, void 0, function* () {
            this.manager = new dummy_manager_1.DummyManager({ bqplot: bqplot });
        });
    });
    it('basics', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x_array = new Float32Array([0, 1]);
            const y_array = new Float32Array([2, 3]);
            const x = { dtype: 'float32', value: new DataView(x_array.buffer) };
            const y = { dtype: 'float32', value: new DataView(y_array.buffer) };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            chai_1.expect(scatter.x.array).to.deep.equal(x_array);
            chai_1.expect(scatter.x.itemSize).to.equal(1);
            chai_1.expect(scatter.x.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(x_array);
            chai_1.expect(scatter.x_previous.itemSize).to.equal(1);
            chai_1.expect(scatter.x_previous.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.y.array).to.deep.equal(y_array);
            chai_1.expect(scatter.y.itemSize).to.equal(1);
            chai_1.expect(scatter.y.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.y_previous.array).to.deep.equal(y_array);
            chai_1.expect(scatter.y_previous.itemSize).to.equal(1);
            chai_1.expect(scatter.y_previous.meshPerAttribute).to.equal(1);
            const new_x = new Float32Array([1, 2]);
            scatter.model.set('x', new_x);
            chai_1.expect(scatter.x.array).to.deep.equal(new_x);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(x_array);
            const new_y = new Float32Array([34, 1]);
            scatter.model.set('y', new_y);
            chai_1.expect(scatter.y.array).to.deep.equal(new_y);
            chai_1.expect(scatter.y_previous.array).to.deep.equal(y_array);
        });
    });
    it('size', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            const default_size = scatter.model.get('default_size');
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(2); // Same size fore the two markers
            chai_1.expect(scatter.size_previous.array).to.deep.equal(scatter.size.array);
            chai_1.expect(scatter.size_previous.meshPerAttribute).to.equal(2);
            scatter.model.set('default_size', default_size * 2);
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size * 2]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.size_previous.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size_previous.meshPerAttribute).to.equal(2);
            // We push an array, the previous should be a scalar
            const new_size = new Float32Array([0, 0.5]);
            scatter.model.set('size', new_size);
            chai_1.expect(scatter.size.array).to.deep.equal(new_size);
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(1); // Each marker has its own size
            chai_1.expect(scatter.size_previous.array).to.deep.equal(new Float32Array([default_size * 2]));
            chai_1.expect(scatter.size_previous.meshPerAttribute).to.equal(2);
            // We push an array that is too short, default_size should be used
            const new_size2 = new Float32Array([0]);
            scatter.model.set('size', new_size2);
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([0, default_size * 2]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.size_previous.array).to.deep.equal(new_size);
            chai_1.expect(scatter.size_previous.meshPerAttribute).to.equal(1);
        });
    });
    it('opacity', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1, 2]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            const opacities = new Float32Array([1]);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.opacity_previous.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity_previous.meshPerAttribute).to.equal(3);
            const opacities2 = new Float32Array([0.5]);
            scatter.model.set('opacities', opacities2);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities2);
            chai_1.expect(scatter.opacity_previous.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(3);
            // We push an array of size 2 for the opacities. We have 3 markers
            // so the opacities should be used periodicly
            scatter.model.set('opacities', new Float32Array([0.5, 1.0]));
            chai_1.expect(scatter.opacity.array).to.deep.equal(new Float32Array([0.5, 1.0, 0.5]));
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.opacity_previous.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.opacity_previous.array).to.deep.equal(opacities2);
            const new_opacity = new Float32Array([0.5, 1.0, 0.8]);
            scatter.model.set('opacity', new_opacity);
            chai_1.expect(scatter.opacity.array).to.deep.equal(new_opacity);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.opacity_previous.array).to.deep.equal(new Float32Array([0.5, 1.0, 0.5]));
            chai_1.expect(scatter.opacity_previous.meshPerAttribute).to.equal(1);
            // We push an array that is too short, opacities should be used
            scatter.model.set('opacity', new Float32Array([0.8]));
            chai_1.expect(scatter.opacity.array).to.deep.equal(new Float32Array([0.8, 1.0, 0.5]));
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.opacity_previous.array).to.deep.equal(new_opacity);
            chai_1.expect(scatter.opacity_previous.meshPerAttribute).to.equal(1);
        });
    });
    it('colors', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1, 2, 1]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3, 4, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            const red = new Float32Array([1, 0, 0]);
            const blue = new Float32Array([0, 0, 1]);
            const steelblue = new Float32Array([
                0.27450981736183167,
                0.5098039507865906,
                0.7058823704719543,
            ]);
            chai_1.expect(scatter.color.array).to.deep.equal(steelblue);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(4);
            chai_1.expect(scatter.scatter_material.defines['USE_COLORMAP']).to.be.false;
            scatter.model.set('colors', ['red']);
            chai_1.expect(scatter.color.array).to.deep.equal(red);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(4);
            chai_1.expect(scatter.scatter_material.defines['USE_COLORMAP']).to.be.false;
            // We push an array of two colors. We have 4 markers
            // so the colors should be used periodicly
            scatter.model.set('colors', ['red', 'blue']);
            chai_1.expect(scatter.color.array).to.deep.equal(new Float32Array([
                red[0],
                red[1],
                red[2],
                blue[0],
                blue[1],
                blue[2],
                red[0],
                red[1],
                red[2],
                blue[0],
                blue[1],
                blue[2],
            ]));
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.scatter_material.defines['USE_COLORMAP']).to.be.false;
            const new_color = new Float32Array([0.5, 1, 0.3, 0.1]);
            scatter.model.set('color', new_color);
            chai_1.expect(scatter.color.array).to.deep.equal(new_color);
            chai_1.expect(scatter.color.itemSize).to.equal(1);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.scatter_material.defines['USE_COLORMAP']).to.be.true;
            chai_1.expect(scatter.scatter_material.uniforms['domain_color'].value).to.deep.equal([new_color[3], new_color[1]]);
            const new_color2 = new Float32Array([0.5, 2, 0.6, -0.3]);
            scatter.model.set('color', new_color2);
            chai_1.expect(scatter.scatter_material.uniforms['domain_color'].value).to.deep.equal([new_color2[3], new_color2[1]]);
            scatter.model.set('color', null);
            // TODO: now the min/max are undefined, or is there a good default?
            scatter.scales.color.model.set('min', 1);
            scatter.scales.color.model.set('max', 3);
            chai_1.expect(scatter.scatter_material.uniforms['domain_color'].value).to.deep.equal([1, 3]);
            scatter.scales.color.model.set('min', -1);
            chai_1.expect(scatter.scatter_material.uniforms['domain_color'].value).to.deep.equal([-1, 3]);
        });
    });
    it('selection', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.scatter_material.uniforms['has_selection'].value).to.be
                .false;
            // 2 is 'out of bounds', we only have two markers
            scatter.model.set('selected', [1, 2]);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0, 1]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(1);
            chai_1.expect(scatter.scatter_material.uniforms['has_selection'].value).to.be.true;
            scatter.model.set('selected', [0, 1]);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([1, 1]));
            chai_1.expect(scatter.scatter_material.uniforms['has_selection'].value).to.be.true;
            scatter.model.set('selected', [0]);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([1, 0]));
            chai_1.expect(scatter.scatter_material.uniforms['has_selection'].value).to.be.true;
        });
    });
    it('rotation', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0, 1]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2, 3]).buffer),
            };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            chai_1.expect(scatter.rotation.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.rotation.itemSize).to.equal(1);
            chai_1.expect(scatter.rotation.meshPerAttribute).to.equal(2);
            scatter.model.set('rotation', [60, 180]);
            chai_1.expect(scatter.rotation.array).to.deep.equal(new Float32Array([60, 180]));
            chai_1.expect(scatter.rotation.itemSize).to.equal(1);
            chai_1.expect(scatter.rotation.meshPerAttribute).to.equal(1);
        });
    });
    it('markers number increase', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x_array = new Float32Array([0, 1]);
            const y_array = new Float32Array([2, 3]);
            const x = { dtype: 'float32', value: new DataView(x_array.buffer) };
            const y = { dtype: 'float32', value: new DataView(y_array.buffer) };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            const steelblue = new Float32Array([
                0.27450981736183167,
                0.5098039507865906,
                0.7058823704719543,
            ]);
            const default_size = scatter.model.get('default_size');
            const opacities = new Float32Array([1]);
            chai_1.expect(scatter.markers_number).to.equal(2);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(2);
            // Checking size/opacity/color/selected attributes
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.color.array).to.deep.equal(steelblue);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(2);
            // Increasing x length
            // `x_previous` should have the right new size, but the markers
            // should not increase until y has increased
            const new_x = new Float32Array([3, 2, 10]);
            scatter.model.set('x', new_x);
            chai_1.expect(scatter.x.array).to.deep.equal(new_x);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(new Float32Array([0, 1, 10]));
            chai_1.expect(scatter.markers_number).to.equal(2);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(2);
            // Increasing y length
            // the markers number should be updated
            const new_y = new Float32Array([4, 2, 2]);
            scatter.model.set('y', new_y);
            chai_1.expect(scatter.x.array).to.deep.equal(new_x);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(new Float32Array([0, 1, 10]));
            chai_1.expect(scatter.y.array).to.deep.equal(new_y);
            chai_1.expect(scatter.y_previous.array).to.deep.equal(new Float32Array([2, 3, 2]));
            chai_1.expect(scatter.markers_number).to.equal(3);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(3);
            // Checking size/opacity/color/selected attributes
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.color.array).to.deep.equal(steelblue);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(3);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(3);
        });
    });
    it('markers number decrease', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x_array = new Float32Array([0, 1]);
            const y_array = new Float32Array([2, 3]);
            const x = { dtype: 'float32', value: new DataView(x_array.buffer) };
            const y = { dtype: 'float32', value: new DataView(y_array.buffer) };
            const objects = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            const scatter = objects.scatter;
            const steelblue = new Float32Array([
                0.27450981736183167,
                0.5098039507865906,
                0.7058823704719543,
            ]);
            const default_size = scatter.model.get('default_size');
            const opacities = new Float32Array([1]);
            chai_1.expect(scatter.markers_number).to.equal(2);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(2);
            // Checking size/opacity/color/selected attributes
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.color.array).to.deep.equal(steelblue);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(2);
            // Decreasing x length
            // it is fine if `x_previous` has not the right size, but the markers number
            // should decrease
            const new_x = new Float32Array([3]);
            scatter.model.set('x', new_x);
            chai_1.expect(scatter.x.array).to.deep.equal(new_x);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(new Float32Array([0, 1]));
            chai_1.expect(scatter.markers_number).to.equal(1);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(1);
            // Checking size/opacity/color/selected attributes
            // The meshPerAttribute parameter should not change, as it would trigger
            // a creation of a new attribute (ThreeJS does not support dynamic
            // meshPerAttribute change on an Attribute)
            chai_1.expect(scatter.size.array).to.deep.equal(new Float32Array([default_size]));
            chai_1.expect(scatter.size.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.opacity.array).to.deep.equal(opacities);
            chai_1.expect(scatter.opacity.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.color.array).to.deep.equal(steelblue);
            chai_1.expect(scatter.color.itemSize).to.equal(3);
            chai_1.expect(scatter.color.meshPerAttribute).to.equal(2);
            chai_1.expect(scatter.selected.array).to.deep.equal(new Float32Array([0]));
            chai_1.expect(scatter.selected.itemSize).to.equal(1);
            chai_1.expect(scatter.selected.meshPerAttribute).to.equal(2);
            // Decreasing y length
            const new_y = new Float32Array([4]);
            scatter.model.set('y', new_y);
            chai_1.expect(scatter.x.array).to.deep.equal(new_x);
            chai_1.expect(scatter.x_previous.array).to.deep.equal(new Float32Array([0, 1]));
            chai_1.expect(scatter.y.array).to.deep.equal(new_y);
            chai_1.expect(scatter.y_previous.array).to.deep.equal(new Float32Array([2, 3]));
            chai_1.expect(scatter.markers_number).to.equal(1);
            chai_1.expect(scatter.instanced_geometry.maxInstancedCount).to.equal(1);
        });
    });
});
//# sourceMappingURL=scatter-mega.js.map