import { ConfigManager } from './config-manager.js';
import { EntityDataFetcher } from './entity-data-fetcher.js';
import { IframeMessenger } from './iframe-messenger.js';
import { EditorUI } from './editor-ui.js';
import { EditorHandlers } from './editor-handlers.js';
/**
* Main card component that integrates with Home Assistant
*/
class MapBadgeCard extends HTMLElement {
constructor() {
super();
this._configManager = new ConfigManager();
this._dataFetcher = new EntityDataFetcher();
this._messenger = new IframeMessenger();
this._updateInterval = null;
this._retryInterval = null;
this._pendingData = null;
this._iframe = null;
}
setConfig(config) {
console.log('[Card] setConfig called with:', JSON.stringify(config, null, 2));
const oldConfig = this._configManager.getConfig();
const newConfig = this._configManager.setConfig(config);
// Check if activity_source changed
const activitySourceChanged = this._configManager.hasChanged(oldConfig, ['activity_source']);
// Configure modules
this._dataFetcher.setDebugMode(newConfig.debug);
this._dataFetcher.setEntities(newConfig.entities);
this._messenger.setDebugMode(newConfig.debug);
// Check if only visual config changed
const visualPropsChanged = this._configManager.hasChanged(oldConfig, [
'zones',
'activities',
'marker_border_radius',
'badge_border_radius',
'marker_size'
]);
if (visualPropsChanged && this._iframe && this._messenger.isReady()) {
this._sendConfigUpdate();
} else {
this._render();
}
// If activity_source changed, trigger data fetch immediately
if (activitySourceChanged && this._updateInterval) {
this._fetchEntities();
}
}
set hass(hass) {
this._dataFetcher.setHass(hass);
// Start fetching entity data when hass is available
if (hass && !this._updateInterval) {
this._startEntityUpdates();
}
// If we have pending data and iframe is ready, send it
if (hass && this._messenger.isReady() && this._pendingData) {
this._messenger.sendData(this._pendingData);
this._pendingData = null;
}
}
_startEntityUpdates() {
const config = this._configManager.getConfig();
// Fetch entities immediately
this._fetchEntities();
// Set up interval for updates
if (this._updateInterval) {
clearInterval(this._updateInterval);
}
this._updateInterval = setInterval(() => {
this._fetchEntities();
}, config.update_interval * 1000);
}
async _fetchEntities() {
const config = this._configManager.getConfig();
const data = await this._dataFetcher.fetchEntities(config);
if (!data) return;
if (this._messenger.isReady()) {
this._messenger.sendData(data);
} else {
this._pendingData = data;
}
}
_sendConfigUpdate() {
const config = this._configManager.getConfig();
this._messenger.sendConfigUpdate(
config.zones,
config.activities,
config.marker_border_radius,
config.badge_border_radius,
config.marker_size
);
}
_render() {
const config = this._configManager.getConfig();
if (!config) return;
const params = this._configManager.buildIframeParams();
const iframeUrl = `/local/map-badge-card/map-badge-v2.html?${params.toString()}`;
this.innerHTML = `