import type { TenantSummary } from "../../../lib/adminApi";

const companyParentTypes = new Set(["COMPANY", "COMPANY_GROUP"]);

export function filterParentTenants(
  tenants: TenantSummary[],
  search: string,
  companyOnly: boolean,
  excludeTenantId = "",
) {
  const normalizedSearch = search.trim().toLowerCase();
  return tenants.filter((tenant) => {
    if (excludeTenantId && tenant.id === excludeTenantId) return false;
    if (companyOnly && !companyParentTypes.has(tenant.type)) return false;
    if (!normalizedSearch) return true;

    return [tenant.name, tenant.slug, tenant.type]
      .filter(Boolean)
      .some((value) => value.toLowerCase().includes(normalizedSearch));
  });
}
