/**
 * Bridge : expose la config des 21 niveaux (injectée par le plugin via avisionLevelsConfig)
 * en tant que AvisionLevelsData pour compatibilité avec premium-dashboard.js.
 * Le thème ne gère que l'affichage (CSS) ; la logique vient du plugin.
 */
(function(global) {
    'use strict';

    var config = typeof global.avisionLevelsConfig !== 'undefined' ? global.avisionLevelsConfig : {};

    var sprintsConfig = config.sprintsConfig || {};
    var monthsPlan = config.monthsPlan || {};
    var infusionTimerHours = config.infusionTimerHours || {};
    var EXCLUDED_FROM_SPRINTS = config.EXCLUDED_FROM_SPRINTS || ['level4', 'level5'];
    var draftMissionsI18n = config.draftMissionsI18n || {};
    var draftMissions = config.draftMissions || draftMissionsI18n.fr || {};
    var DRAFT_EXCLUDED_WHEN_GUARDIAN = config.DRAFT_EXCLUDED_WHEN_GUARDIAN || [];
    var verticalLocks = config.verticalLocks && typeof config.verticalLocks === 'object' ? config.verticalLocks : {};

    function getDraftMissions(lang) {
        lang = lang || 'en';
        return draftMissionsI18n[lang] || draftMissionsI18n.en || draftMissions;
    }
    var sprintKeysOrder = config.sprintKeysOrder || ['sprint1', 'sprint2', 'sprint3', 'sprint4', 'sprint5', 'sprint6', 'sprint7'];
    var sprintLevelIds = config.sprintLevelIds || [];

    function getInfusionTimerHours(levelId) {
        return infusionTimerHours[levelId] != null ? infusionTimerHours[levelId] : 72;
    }

    function getSprintLevelIds() {
        return sprintLevelIds.length ? sprintLevelIds : (function() {
            var ids = [];
            sprintKeysOrder.forEach(function(key) {
                if (sprintsConfig[key] && sprintsConfig[key].levels) {
                    sprintsConfig[key].levels.forEach(function(id) { ids.push(id); });
                }
            });
            return ids;
        })();
    }

    function getSprintIndexForLevel(levelId) {
        if (EXCLUDED_FROM_SPRINTS.indexOf(levelId) !== -1) return 0;
        for (var i = 0; i < sprintKeysOrder.length; i++) {
            var levels = sprintsConfig[sprintKeysOrder[i]] && sprintsConfig[sprintKeysOrder[i]].levels;
            if (levels && levels.indexOf(levelId) !== -1) return i + 1;
        }
        return 0;
    }

    function getSprintLevelsByIndex(sprintIndex) {
        if (sprintIndex < 1 || sprintIndex > sprintKeysOrder.length) return [];
        var key = sprintKeysOrder[sprintIndex - 1];
        return (sprintsConfig[key] && sprintsConfig[key].levels) ? sprintsConfig[key].levels : [];
    }

    var AvisionLevelsData = {
        sprintsConfig: sprintsConfig,
        monthsPlan: monthsPlan,
        infusionTimerHours: infusionTimerHours,
        EXCLUDED_FROM_SPRINTS: EXCLUDED_FROM_SPRINTS,
        getInfusionTimerHours: getInfusionTimerHours,
        getSprintLevelIds: getSprintLevelIds,
        getSprintIndexForLevel: getSprintIndexForLevel,
        getSprintLevelsByIndex: getSprintLevelsByIndex,
        draftMissions: draftMissions,
        draftMissionsI18n: draftMissionsI18n,
        getDraftMissions: getDraftMissions,
        DRAFT_EXCLUDED_WHEN_GUARDIAN: DRAFT_EXCLUDED_WHEN_GUARDIAN,
        verticalLocks: verticalLocks
    };

    if (typeof module !== 'undefined' && module.exports) {
        module.exports = AvisionLevelsData;
    } else {
        global.AvisionLevelsData = AvisionLevelsData;
    }
})(typeof window !== 'undefined' ? window : this);
