"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 = require("..");
// import {Figure} from '../src/Figure.js';
// import {FigureModel} from '../src/FigureModel.js';
const widget_utils_1 = require("./widget-utils");
const d3Timer = __importStar(require("d3-timer"));
// text pixel coordinate
const test_x = 200;
const test_y = 200;
const pixel_red = [255, 0, 0, 255];
const pixel_blue = [0, 0, 255, 255];
describe('figure >', () => {
    beforeEach(function () {
        return __awaiter(this, void 0, void 0, function* () {
            this.manager = new dummy_manager_1.DummyManager({ bqplot: bqplot });
        });
    });
    it('should not create a WebGL renderer if not needed', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0.5, 0.5]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2.0, 2.5]).buffer),
            };
            const { figure } = yield widget_utils_1.create_figure_scatter(this.manager, x, y);
            chai_1.expect(figure.renderer).to.be.undefined;
        });
    });
    it('should create a WebGL renderer when needed', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0.5, 0.5]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2.0, 2.5]).buffer),
            };
            const { figure } = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            chai_1.expect(figure.renderer).to.not.be.undefined;
        });
    });
    it('canvas/png render check', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0.5, 0.5]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2.0, 2.5]).buffer),
            };
            const { scatter, figure } = yield widget_utils_1.create_figure_scatter(this.manager, x, y);
            // we render a huge red scatter point, and check if the path of svg->canvas (and thus
            // png) results in a red pixels at the test coordinates.
            scatter.model.set('default_size', 1e6);
            scatter.model.set('colors', ['red']);
            scatter.d3el.selectAll('.object_grp').data();
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            const canvas = yield figure.get_rendered_canvas();
            const context = canvas.getContext('2d');
            const pixel = context.getImageData(test_x, test_y, 1, 1);
            chai_1.expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red);
        });
    });
    it('canvas/png render gl check', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0.5, 0.5]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2.0, 2.5]).buffer),
            };
            const { scatter: scatterGlView, figure } = yield widget_utils_1.create_figure_scatter(this.manager, x, y, true);
            // we render a huge red scatter point, and check if the path of svg->canvas (and thus
            // png) results in a red pixels at the test coordinates.
            scatterGlView.model.set('default_size', 1e6);
            scatterGlView.model.set('colors', ['red']);
            // scatterGlView.d3el.selectAll(".object_grp").data();
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            let canvas = yield figure.get_rendered_canvas();
            let context = canvas.getContext('2d');
            let pixel = context.getImageData(test_x, test_y, 1, 1);
            chai_1.expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red);
            // add a normal (blue) scatter, which should be overlayed on top
            const scales = {
                x: scatterGlView.model.get('scales').x.toJSON(),
                y: scatterGlView.model.get('scales').y.toJSON(),
            };
            const color = null;
            const size = null;
            const opacity = null;
            const rotation = null;
            const skew = null;
            const scatter = yield widget_utils_1.create_model_bqplot(this.manager, 'Scatter', 'scatter1', {
                scales: scales,
                x: x,
                y: y,
                color: color,
                size: size,
                opacity: opacity,
                rotation: rotation,
                skew: skew,
                colors: ['blue'],
                visible: true,
                default_size: 1e6,
                selected_style: {},
                unselected_style: {},
                hovered_style: {},
                unhovered_style: {},
                preserve_domain: {},
                _view_module_version: '*',
                _view_module: 'bqplot',
            });
            figure.model.set('marks', [scatter, scatterGlView.model]);
            yield Promise.all(figure.mark_views.views);
            d3Timer.timerFlush(); // this makes sure the animations are all executed
            canvas = yield figure.get_rendered_canvas();
            context = canvas.getContext('2d');
            pixel = context.getImageData(test_x, test_y, 1, 1);
            chai_1.expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_blue);
        });
    });
    it('marks removed before created', function () {
        return __awaiter(this, void 0, void 0, function* () {
            const x = {
                dtype: 'float32',
                value: new DataView(new Float32Array([0.5, 0.5]).buffer),
            };
            const y = {
                dtype: 'float32',
                value: new DataView(new Float32Array([2.0, 2.5]).buffer),
            };
            const { scatter, figure } = yield widget_utils_1.create_figure_scatter(this.manager, x, y);
            // we start with the scatter and legend
            chai_1.expect(figure.fig_marks.node().children).lengthOf(2);
            // we remove the scatter
            figure.model.set('marks', []);
            yield Promise.all(figure.mark_views.views);
            chai_1.expect(figure.fig_marks.node().children).lengthOf(1);
            // we set the marks twice without waiting, so we should never see the second scatter in the DOM
            figure.model.set('marks', [scatter.model, scatter.model]);
            const previousViewsPromise = Promise.all(figure.mark_views.views);
            figure.model.set('marks', [scatter.model]);
            yield Promise.all(figure.mark_views.views);
            yield previousViewsPromise; // we also want to wait for these promises to be resolved so the dummy
            // DOM node van be removed in the remove event handler in Figure.add_mark
            chai_1.expect(figure.fig_marks.node().children).lengthOf(2);
        });
    });
});
//# sourceMappingURL=figure.js.map