import type { StoreSiteBlock, StoreSitePage } from '@alqove/api-client';

/** Local, editable copy of one page while the seller works on it. */
export interface PageDraft {
  title: string;
  slug: string;
  navLabel: string;
  showInNav: boolean;
  isPublished: boolean;
  seoTitle: string;
  seoDescription: string;
  blocks: StoreSiteBlock[];
}

export function toDraft(page: StoreSitePage): PageDraft {
  return {
    title: page.title ?? '',
    slug: page.slug ?? '',
    navLabel: page.nav_label ?? '',
    showInNav: page.show_in_nav ?? true,
    isPublished: page.is_published ?? true,
    seoTitle: page.seo_title ?? '',
    seoDescription: page.seo_description ?? '',
    blocks: page.blocks ?? [],
  };
}

/**
 * Identity of the server copy a draft was taken from. `updated_at` changes on
 * every save, which is exactly when the editor should adopt the server's
 * normalised values (slugified addresses, block ids assigned on insert).
 */
export function pageRevision(page: StoreSitePage | null | undefined): string | null {
  return page?.id ? `${page.id}:${page.updated_at ?? ''}` : null;
}
