// calc_config.js - applies CalcBuilder layout to active calc views
(function () {
var VIEW_IDS = [
'solarConfigView',
'batteriConfigView',
'laddboxConfigView',
'taktvatConfigView',
'varmepumpConfigView',
'takConfigView',
'isoleringConfigView',
'fonsterConfigView',
'summaryProductList'
];
function visible(el) {
return !!el && el.style.display !== 'none';
}
function currentCategory() {
var sel = document.getElementById('categorySelect');
return sel && sel.value ? sel.value : null;
}
function currentSchema() {
var categoryId = window.__calcLayoutCurrentCategory || currentCategory();
if (!categoryId || typeof window.getCalcSchema !== 'function') return null;
return window.getCalcSchema(categoryId);
}
function moduleEnabled(list, key, fallback) {
if (!Array.isArray(list) || !list.length) return fallback;
return list.indexOf(key) !== -1;
}
function findLayoutWrap(viewId) {
var view = document.getElementById(viewId);
if (!visible(view)) return null;
var first = view.firstElementChild;
if (!first) return null;
if (first.tagName === 'DIV') return first;
return null;
}
function applySidebarPosition(schema) {
var sidebarPosition = schema && schema.sidebar_position === 'left' ? 'left' : 'right';
VIEW_IDS.forEach(function (viewId) {
var wrap = findLayoutWrap(viewId);
if (!wrap) return;
wrap.classList.add('calc-config-layout');
wrap.classList.remove('is-sidebar-left', 'is-sidebar-right');
wrap.classList.add(sidebarPosition === 'left' ? 'is-sidebar-left' : 'is-sidebar-right');
});
}
function applyHeadModules(schema) {
var head = schema && schema.regions ? schema.regions.head : [];
var showCustomer = moduleEnabled(head, 'customer-block', true);
var showPhotos = moduleEnabled(head, 'prospect-images', true);
var showTitle = moduleEnabled(head, 'calc-title', true);
var customer = document.getElementById('kalkylCustomerBanner');
var photos = document.getElementById('kalkylPhotosSection');
var title = document.getElementById('kalkylTitle');
var titleWrap = title && title.parentElement ? title.parentElement : null;
if (customer) customer.style.display = showCustomer ? '' : 'none';
if (photos) photos.style.display = showPhotos ? '' : 'none';
if (titleWrap) titleWrap.style.display = showTitle ? '' : 'none';
}
function applyNewCalcContent(schema) {
var content = schema && schema.regions ? schema.regions.content : [];
var showGrid = moduleEnabled(content, 'category-grid', true);
var categoryGrid = document.getElementById('cfgCategoryGrid');
var categoryBar = document.getElementById('cfgCategoryBar');
if (!categoryGrid || !categoryBar) return;
if (!currentCategory()) {
categoryBar.style.display = showGrid ? 'flex' : 'none';
categoryGrid.style.display = showGrid ? 'grid' : 'none';
}
}
function applySchemaLayout() {
var schema = currentSchema();
applyHeadModules(schema);
applySidebarPosition(schema);
applyNewCalcContent(schema);
}
function patchHooks() {
if (window.__calcConfigLayoutPatched) return true;
if (typeof window.newCalc !== 'function' || typeof window.changeCategory !== 'function') return false;
var originalNewCalc = window.newCalc;
window.newCalc = function (kundData) {
window.__calcLayoutCurrentCategory = null;
var result = originalNewCalc.apply(this, arguments);
Promise.resolve(typeof window.loadCalcSchemasFromAPI === 'function' ? window.loadCalcSchemasFromAPI() : null)
.finally(function () {
setTimeout(applySchemaLayout, 0);
setTimeout(applySchemaLayout, 120);
});
return result;
};
var originalChangeCategory = window.changeCategory;
window.changeCategory = function () {
var result = originalChangeCategory.apply(this, arguments);
window.__calcLayoutCurrentCategory = currentCategory();
Promise.resolve(typeof window.loadCalcSchemasFromAPI === 'function' ? window.loadCalcSchemasFromAPI() : null)
.finally(function () {
setTimeout(applySchemaLayout, 0);
setTimeout(applySchemaLayout, 120);
});
return result;
};
if (typeof window.showKalkylSummary === 'function') {
var originalShowKalkylSummary = window.showKalkylSummary;
window.showKalkylSummary = function () {
var result = originalShowKalkylSummary.apply(this, arguments);
setTimeout(applySchemaLayout, 0);
setTimeout(applySchemaLayout, 120);
return result;
};
}
if (typeof window.showKalkylConfig === 'function') {
var originalShowKalkylConfig = window.showKalkylConfig;
window.showKalkylConfig = function () {
var result = originalShowKalkylConfig.apply(this, arguments);
setTimeout(applySchemaLayout, 0);
return result;
};
}
window.__calcConfigLayoutPatched = true;
return true;
}
function boot() {
if (typeof window.loadCalcSchemasFromAPI === 'function') {
window.loadCalcSchemasFromAPI();
}
if (patchHooks()) {
applySchemaLayout();
return;
}
var tries = 0;
var timer = setInterval(function () {
tries += 1;
if (patchHooks() || tries > 100) {
clearInterval(timer);
applySchemaLayout();
}
}, 100);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot);
} else {
boot();
}
})();