"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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;
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BarChart = void 0;
var echarts = __importStar(require("echarts"));
var mainTemplate_1 = __importDefault(require("./mainTemplate")); // Import your HTML template
var BarChart = /** @class */ (function () {
    function BarChart() {
        this.chartInstance = null;
        // You can perform any initial setup here if needed
    }
    BarChart.prototype.getTemplate = function () {
        return __awaiter(this, void 0, void 0, function () {
            var template, containerId, chartHTML, replacedTemplate, finalTemplate;
            return __generator(this, function (_a) {
                template = (0, mainTemplate_1.default)();
                containerId = "barChart";
                chartHTML = this.renderChart(containerId);
                replacedTemplate = template.replace("<bar-chart></bar-chart>", "<img src=\"".concat(chartHTML, "\" alt=\"Bar Chart\" />"));
                finalTemplate = replacedTemplate.replace('<growth-chart></growth-chart>', "<img src=\"".concat(chartHTML, "\" alt=\"Bar Chart\" />"));
                // Ensure the chart is rendered in the DOM after inserting the final template
                this.initializeChart(containerId);
                return [2 /*return*/, finalTemplate]; // Return the final modified template
            });
        });
    };
    BarChart.prototype.createGradient = function (colorStart, colorEnd) {
        return {
            type: "linear",
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
                { offset: 0, color: colorStart },
                { offset: 1, color: colorEnd },
            ],
        };
    };
    BarChart.prototype.getGradientColor = function (value) {
        if (value >= 7) {
            return this.createGradient("#6930C3", "#5E60CE"); // High Risk Gradient
        }
        else if (value >= 5) {
            return this.createGradient("#7400B8", "#4EA8DE"); // Medium Risk Gradient
        }
        return this.createGradient("#4CC9F0", "#90E0EF"); // Low Risk Gradient
    };
    BarChart.prototype.renderChart = function (containerId) {
        var _this = this;
        // Create the chart container
        var chartContainer = document.createElement("div");
        chartContainer.id = containerId;
        chartContainer.style.position = "absolute"; // Added for absolute positioning
        chartContainer.style.left = "0px";
        chartContainer.style.top = "0px";
        chartContainer.style.width = "500px";
        chartContainer.style.height = "400px";
        chartContainer.style.userSelect = "none"; // Optional, to prevent text selection
        chartContainer.style.padding = "0px";
        chartContainer.style.margin = "0px";
        chartContainer.style.borderWidth = "0px";
        // Initialize the ECharts instance
        this.chartInstance = echarts.init(chartContainer);
        // Set up the chart options using the data from your ProgressChart
        var dataAxis = ["Consolidated", "Stable", "Local", "Industry", "Laggers"];
        var dataValues = [6.6, 6.3, 5.7, 5.0, 5.3];
        var getYAxisLabel = function (value) {
            if (value === 10)
                return "{riskLabel|High Risk}\n{innovationLabel|Strong need for \n innovation}";
            if (value === 5)
                return "{riskLabel|Medium Risk}\n{innovationLabel|Need for \n innovation}";
            if (value === 1)
                return "{riskLabel|Low Risk}\n{innovationLabel|Low need for \n innovation}";
        };
        var bottomAxisLabels = [
            "Market maturity",
            "Market situation",
            "Competitors",
            "Competition",
            "Customers",
        ];
        var topAxisLabels = [
            "Fragmented",
            "Volatile",
            "Global",
            "New players",
            "Early adopters",
        ];
        var options = {
            title: { text: "Bar Chart" },
            tooltip: {
                trigger: "axis",
                axisPointer: { type: "shadow" },
                formatter: function (params) {
                    var value = params[0].value;
                    return "Value: ".concat(value, "<br/>Risk Level: ").concat(_this.getRiskLevel(value));
                },
            },
            xAxis: [
                {
                    data: dataAxis,
                    axisLabel: {
                        color: "#3a0c78",
                        fontSize: "12px",
                        fontFamily: "Poppins",
                        fontWeight: 500,
                    },
                    axisTick: { show: false },
                    axisLine: { show: false },
                },
                {
                    data: topAxisLabels,
                    position: "top",
                    axisLabel: {
                        color: "#3a0c78",
                        fontSize: "12px",
                        fontWeight: 500,
                        fontFamily: "Poppins",
                        margin: 10,
                    },
                    axisTick: { show: false },
                    axisLine: { show: false },
                },
                {
                    data: bottomAxisLabels,
                    position: "bottom",
                    axisLabel: {
                        fontFamily: "Poppins",
                        color: "#3a0c78",
                        fontSize: "14px",
                        fontWeight: 600,
                        margin: 40,
                    },
                    axisTick: { show: false },
                    axisLine: { show: false },
                },
            ],
            yAxis: [
                {
                    min: 0,
                    max: 10,
                    axisLine: { show: false },
                    axisTick: { show: false },
                    axisLabel: { color: "#3a0c78" },
                    splitLine: { lineStyle: { color: "#00000019" } },
                },
                {
                    type: "value",
                    position: "right",
                    min: 1,
                    max: 10,
                    axisLabel: {
                        show: true,
                        formatter: function (value) { return getYAxisLabel(value) || ''; },
                        rich: {
                            riskLabel: {
                                color: "#3a0c78",
                                fontWeight: "bold",
                                fontSize: 12,
                                fontFamily: "Poppins",
                            },
                            innovationLabel: {
                                color: "#7a49ab",
                                fontSize: 10,
                                fontFamily: "Poppins",
                            },
                        },
                    },
                    axisLine: { show: false },
                    axisTick: { show: false },
                    splitLine: { show: false },
                },
            ],
            series: [
                {
                    type: "bar",
                    showBackground: true,
                    backgroundStyle: { color: "#bb94e7", borderRadius: [50, 50, 50, 50] },
                    data: [6.6, 6.3, 5.7, 5.0, 5.3],
                    barCategoryGap: "80%",
                    itemStyle: {
                        // Correctly typing the params as CallbackDataParams
                        color: function (params) {
                            return _this.getGradientColor(params.value); // Cast to number
                        },
                        borderRadius: [50, 50, 50, 50],
                    },
                    label: {
                        show: true,
                        position: "top",
                        color: "#3a0c78",
                        fontWeight: "bold",
                        fontFamily: "Poppins",
                        distance: 40,
                    },
                    emphasis: { itemStyle: { color: "#5e37ab" } },
                    animation: true,
                    animationDuration: 5000,
                    animationEasing: "elasticOut",
                },
            ],
        };
        // Set the chart options
        this.chartInstance.setOption(options);
        return this.chartInstance.getDataURL({
            pixelRatio: 8,
            backgroundColor: "#fff",
            type: "png",
        }) || null;
    };
    BarChart.prototype.initializeChart = function (containerId) {
        // Ensure the chart is initialized after the template is loaded
        var chartContainer = document.getElementById(containerId);
        if (chartContainer && this.chartInstance) {
            this.chartInstance.resize(); // Resize the chart to fit the container
            this.chartInstance.setOption(this.chartInstance.getOption()); // Reapply the options
        }
        else {
            console.error("Chart container not found in the DOM or chart instance is null.");
        }
    };
    // Additional helper functions
    BarChart.prototype.getRiskLevel = function (value) {
        if (value >= 7)
            return "High Risk";
        if (value >= 5)
            return "Medium Risk";
        return "Low Risk";
    };
    return BarChart;
}());
exports.BarChart = BarChart;
