"use client";

import { useMemo } from "react";
import { isAccountPublishReady } from "@/lib/social/token-validation";
import { selectActiveAccounts } from "@/store/project-selectors";
import { useProjectsStore } from "@/store/projects-store";

export function useConnectedPlatforms(): string[] {
  const accounts = useProjectsStore(selectActiveAccounts);
  return useMemo(
    () => accounts.filter((a) => isAccountPublishReady(a)).map((a) => a.platform),
    [accounts],
  );
}

export function useAccountIssues(): string[] {
  const accounts = useProjectsStore(selectActiveAccounts);
  return useMemo(() => {
    const issues: string[] = [];
    for (const a of accounts) {
      if (a.connected && a.connectionStatus === "expired") {
        issues.push(`${a.label} token expired`);
      }
      if (a.connected && a.connectionStatus === "error") {
        issues.push(`${a.label} connection error`);
      }
    }
    return issues;
  }, [accounts]);
}
