export interface CurrentProductSummary {
  id: string;
  code: string;
  directionCode: string;
  name: string;
  managerUserId: string;
  statusCode: string;
}

export function resolveObjectIdFromQuery(
  routeObjectId: string | null | Array<string | null> | undefined,
  fallbackObjectId: string
) {
  if (Array.isArray(routeObjectId)) {
    return String(routeObjectId[0] || fallbackObjectId || '');
  }

  if (routeObjectId === null || routeObjectId === undefined || routeObjectId === '') {
    return fallbackObjectId;
  }

  return String(routeObjectId);
}

export function normalizeCurrentProductSummary(
  objectSummary: App.ObjectContext.Summary | null | undefined,
  objectName: string
): CurrentProductSummary | null {
  const currentProduct = objectSummary?.currentProduct;

  if (!currentProduct || typeof currentProduct !== 'object') {
    return null;
  }

  return {
    id: String((currentProduct as Record<string, unknown>).id || ''),
    code: String((currentProduct as Record<string, unknown>).code || ''),
    directionCode: String((currentProduct as Record<string, unknown>).directionCode || ''),
    name: String((currentProduct as Record<string, unknown>).name || objectName || ''),
    managerUserId: String((currentProduct as Record<string, unknown>).managerUserId || ''),
    statusCode: String((currentProduct as Record<string, unknown>).statusCode || '')
  };
}
