function sendFocusState(payload) {
  chrome.runtime.sendMessage({
    source: 'goal-axis',
    type: 'SET_FOCUS_STATE',
    focusActive: Boolean(payload.focusActive),
    taskLabel: payload.taskLabel || '',
    blocklist: Array.isArray(payload.blocklist) ? payload.blocklist : undefined,
    penaltyActive: Boolean(payload.penaltyActive),
    penaltyUntil: typeof payload.penaltyUntil === 'number' ? payload.penaltyUntil : undefined,
    penaltyMessage: payload.penaltyMessage || '',
    penaltyBlocklist: Array.isArray(payload.penaltyBlocklist) ? payload.penaltyBlocklist : undefined,
  });
}

window.addEventListener('message', (event) => {
  if (event.source !== window) return;
  if (!event.data || event.data.source !== 'goal-axis') return;
  if (event.data.type !== 'FOCUS_STATE') return;

  sendFocusState(event.data.payload || {});
});

window.dispatchEvent(new CustomEvent('goal-axis-extension-ready'));
