import { useEffect, useState } from "react";
import { fetchDslMeta, type DslMeta } from "@/api/client";

export type { DslMeta } from "@/api/client";

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;

    fetchDslMeta()
      .then((data) => {
        cachedMeta = data;
        setMeta(data);
      })
      .catch(() => {});
  }, []);

  return meta;
}
