const PanelsSettings = {
  pathType: 'path', // 'hash' | 'path'
  rootPanelList: null, // This will be set to the root panel list when the app is initialized
};

export function getPanelsSettings() {
  return PanelsSettings;
}

export function getRootPanelList() {
  return PanelsSettings.rootPanelList;
}

export function isPathType() {
  return PanelsSettings.pathType === 'path';
}

export function setPanelsPathType(settings: any) {
  PanelsSettings.pathType = settings.pathType ?? 'path';
}

export function setRootPanelList(rootPanelList: any) {
  PanelsSettings.rootPanelList = rootPanelList;
}

export function emitGlobalEvent(eventName: string, data: any) {
  const event = new CustomEvent(`panel:${eventName}`, { detail: data });
  window.dispatchEvent(event);
}

export function onGlobalEvent(eventName: string|string[], callback: (event: CustomEvent) => void) {
  const eventNames = Array.isArray(eventName) ? eventName : [eventName];
  for (const name of eventNames) {
    window.addEventListener(`panel:${name}`, callback);
  }
}

export function offGlobalEvent(eventName: string|string[], callback: (event: CustomEvent) => void) {
  const eventNames = Array.isArray(eventName) ? eventName : [eventName];
  for (const name of eventNames) {
    window.removeEventListener(`panel:${name}`, callback);
  }
}
