import { useEffect, useState } from "react";

export interface DslMeta {
  keywords: string[];
  values: string[];
  commands: { label: string; detail: string; section: string; snippet: string }[];
  themes: string[];
}

const DEFAULT_META: DslMeta = {
  keywords: [],
  values: [],
  commands: [],
  themes: [],
};

let cachedMeta: DslMeta | null = null;

export function useDslMeta(): DslMeta {
  const [meta, setMeta] = useState<DslMeta>(cachedMeta || DEFAULT_META);

  useEffect(() => {
    if (cachedMeta) return;

    fetch("/api/dsl-meta")
      .then((r) => r.json())
      .then((data: DslMeta) => {
        cachedMeta = data;
        setMeta(data);
      })
      .catch(() => {});
  }, []);

  return meta;
}
