"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.chartBuilder = exports.ensureRegistrations = exports.configureChart = exports.buildChart = void 0;
var SciChartSurfaceType_1 = require("../types/SciChartSurfaceType");
var buildAnnotations_1 = require("./buildAnnotations");
var buildAxis_1 = require("./buildAxis");
var buildDataSeries_1 = require("./buildDataSeries");
var buildModifiers_1 = require("./buildModifiers");
var buildSeries_1 = require("./buildSeries");
var buildSurface_1 = require("./buildSurface");
// import { chartReviver } from "./chartReviver";
var classFactory_1 = require("../Core/classFactory");
var chartReviver_1 = require("./helpers/chartReviver");
var getOptionsFromDefinition = function (definition) {
    if ("options" in definition) {
        return definition.options;
    }
    else {
        return definition;
    }
};
/**
 * Builds an entire chart from a definition that can be pure data.
 * @param divElementId The Div Element ID where the surface will reside
 * @param definition a {@link TSurfaceDefinition } or a string which will be parsed to it.
 * @returns A chart of the appropriate type based on the definition
 */
function buildChart(divElementId, definition) {
    var _a;
    if (typeof definition === "string") {
        definition = JSON.parse(definition, chartReviver_1.chartReviver);
    }
    if (!definition) {
        definition = {};
    }
    var type = (_a = definition === null || definition === void 0 ? void 0 : definition.type) !== null && _a !== void 0 ? _a : SciChartSurfaceType_1.ESciChartSurfaceType.Default2D;
    var spreadDefinition = getOptionsFromDefinition(definition);
    if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Default2D) {
        return (0, buildSurface_1.build2DChart)(divElementId, spreadDefinition);
    }
    else if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Polar2D) {
        return (0, buildSurface_1.build2DPolarChart)(divElementId, spreadDefinition);
    }
    else if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Pie2D) {
        return (0, buildSurface_1.buildPieChart)(divElementId, spreadDefinition);
    }
    else if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Default3D) {
        return (0, buildSurface_1.build3DChart)(divElementId, spreadDefinition);
    }
    else {
        throw new Error("Surface type: ".concat(type, " does not exist. See ESciChartSurfaceType for all available types."));
    }
}
exports.buildChart = buildChart;
/**
 * Configures an existing surface using a definition.
 * This is useful if you need to use the wasmContext in methods or classes you use in your definition
 * @param sciChartSurface
 * @param wasmContext The webassemply context.  Pass undefined for a pie surface.
 * @param definition
 */
var configureChart = function (sciChartSurface, wasmContext, definition) {
    var _a;
    if (typeof definition === "string") {
        definition = JSON.parse(definition, chartReviver_1.chartReviver);
    }
    if (!definition) {
        definition = {};
    }
    var type = (_a = definition === null || definition === void 0 ? void 0 : definition.type) !== null && _a !== void 0 ? _a : SciChartSurfaceType_1.ESciChartSurfaceType.Default2D;
    var spreadDefinion = getOptionsFromDefinition(definition);
    if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Default2D) {
        (0, buildSurface_1.configure2DSurface)(spreadDefinion, sciChartSurface, wasmContext);
    }
    else if (type === SciChartSurfaceType_1.ESciChartSurfaceType.Pie2D) {
        // await return buildPieChart(divElementId, definition.options);
    }
    else {
        // @ts-ignore
        throw new Error("Surface type: ".concat(type, " is not yet supported by the builder api"));
    }
};
exports.configureChart = configureChart;
/**
 * This is just something to call to ensure that all the registrations are run before a surface is created
 */
var ensureRegistrations = function () { };
exports.ensureRegistrations = ensureRegistrations;
exports.chartBuilder = {
    /** @inheritdoc */
    buildChart: buildChart,
    /** @inheritdoc */
    chartReviver: chartReviver_1.chartReviver,
    /** @inheritdoc */
    build2DChart: buildSurface_1.build2DChart,
    /** @inheritdoc */
    build2DPolarChart: buildSurface_1.build2DPolarChart,
    /** @inheritdoc */
    buildPieChart: buildSurface_1.buildPieChart,
    /** @inheritdoc */
    configureChart: exports.configureChart,
    /** @inheritdoc */
    buildSeries: buildSeries_1.buildSeries,
    /** @inheritdoc */
    buildDataSeries: buildDataSeries_1.buildDataSeries,
    /** @inheritdoc */
    buildModifiers: buildModifiers_1.buildModifiers,
    /** @inheritdoc */
    buildAxes: buildAxis_1.buildAxes,
    /** @inheritdoc */
    buildAnnotations: buildAnnotations_1.buildAnnotations,
    /** @inheritdoc */
    registerType: classFactory_1.registerType,
    /** @inheritdoc */
    registerWasmType: classFactory_1.registerWasmType,
    /** @inheritdoc */
    registerFunction: classFactory_1.registerFunction,
    /** @inheritdoc */
    build3DChart: buildSurface_1.build3DChart,
    /** @inheritdoc */
    buildAxis3D: buildAxis_1.buildAxis3D,
    /** @inheritdoc */
    buildModifiers3D: buildModifiers_1.buildModifiers3D,
    /** @inheritdoc */
    buildSeries3D: buildSeries_1.buildSeries3D,
    /** @inheritdoc */
    buildDataSeries3D: buildDataSeries_1.buildDataSeries3D
};
