import { SearchAttributes } from '@spiffy-ai/commerce-api-client';

export const isApiSearchAttributes = (data: unknown): data is SearchAttributes => {
  if (data == null || typeof data !== 'object') {
    return false;
  }

  if (!('search_term' in data) || typeof data.search_term !== 'string') {
    return false;
  }
  if ('selected_filters' in data && !Array.isArray(data.selected_filters)) {
    return false;
  }

  if (data.search_term) {
    return true;
  }

  if (
    !('search_results' in data) ||
    !Array.isArray(data.search_results) ||
    !data.search_results.every(v => typeof v === 'string')
  ) {
    return false;
  }

  return true;
};
