import type { TemplateResult } from 'lit';

export type TTemplateFactory = () => TemplateResult | Promise<TemplateResult>;

// Demo can be a single function or an array of functions
export type TDemoDefinition = TTemplateFactory | TTemplateFactory[];

export const resolveTemplateFactory = async (
  factoryArg: TTemplateFactory
): Promise<TemplateResult> => {
  return await Promise.resolve(factoryArg());
};

/**
 * Get the number of demos for an element
 */
export const getDemoCount = (demo: TDemoDefinition): number => {
  if (Array.isArray(demo)) {
    return demo.length;
  }
  return 1;
};

/**
 * Get a specific demo by index (0-based internally, displayed as 1-based)
 */
export const getDemoAtIndex = (demo: TDemoDefinition, index: number): TTemplateFactory | null => {
  if (Array.isArray(demo)) {
    return demo[index] ?? null;
  }
  // Single demo - only index 0 is valid
  return index === 0 ? demo : null;
};

/**
 * Check if an element has multiple demos
 */
export const hasMultipleDemos = (demo: TDemoDefinition): boolean => {
  return Array.isArray(demo) && demo.length > 1;
};
