import { type IssueResponse } from '@/client';

export type RepoLike = {
    uid?: string;
    repo_uid?: string;
    repo_name: string;
    namespace?: string;
    description?: string | null;
    is_private?: boolean;
    star_count?: number;
    watch_count?: number;
    commit_count?: number;
    branch_count?: number;
    tag_count?: number;
    default_branch?: string;
    last_commit_at?: string;
};

export type IssueListResponseLike = {
    items?: IssueResponse[];
    issues?: IssueResponse[];
    total?: number;
};

export type MultiFilter = {
    statuses: string[];
    search: string;
};

export const STATUS_OPTIONS = ['open', 'closed'];

export const EMPTY_FILTER: MultiFilter = {
    statuses: [],
    search: '',
};

export const normalizeIssueList = (value: unknown): { items: IssueResponse[]; total: number } => {
    if (Array.isArray(value)) return { items: value as IssueResponse[], total: value.length };
    const payload = (value || {}) as IssueListResponseLike;
    const items = payload.items || payload.issues || [];
    return { items, total: payload.total ?? items.length };
};

export const getRepoUid = (repo: RepoLike): string =>
    repo.uid || repo.repo_uid || repo.repo_name;

export const getViewStorageKey = (projectName: string) =>
    `project-issues-saved-views:${projectName}`;
