import {
  getVirtualStickerWindow,
  staticStickerOriginalUrl,
  staticStickerThumbnailUrl,
  type StaticStickerCatalogItem,
  type VirtualStickerWindow,
} from "@dada/static-sticker-catalog";
import { P0A_STATIC_STICKER_RELEASE_VERSION } from "@dada/template-registry";

const resourceVersion = P0A_STATIC_STICKER_RELEASE_VERSION;
const partCounts = [203, 36, 27, 48, 38, 75, 37, 67, 48, 24, 40, 30, 27, 51, 62, 19, 36, 45, 92, 53, 69, 31, 36, 30, 183] as const;

function buildCatalog(): StaticStickerCatalogItem[] {
  let index = 0;
  return partCounts.flatMap((count, partIndex) => Array.from({ length: count }, (_, orderIndex) => {
    index += 1;
    const stableId = `STK${String(index).padStart(index < 1_000 ? 3 : 4, "0")}`;
    const item = {
      enabled: true,
      height: 0,
      mime: "image/png" as const,
      mime_type: "image/png" as const,
      order: orderIndex + 1,
      original_filename: `${stableId}.png`,
      original_reference: "",
      origin: "bundled_read_only" as const,
      part: partIndex + 1,
      relative_path: `sticker_part${partIndex + 1}/${stableId}.png`,
      resource_version: resourceVersion,
      sha256: "",
      stable_id: stableId,
      thumbnail_reference: { media: "thumbnail" as const, resource_id: stableId, resource_version: resourceVersion, url: "" },
      width: 0,
    } satisfies StaticStickerCatalogItem;
    item.original_reference = staticStickerOriginalUrl(item);
    item.thumbnail_reference.url = staticStickerThumbnailUrl(item);
    return item;
  }));
}

export const P0A_STATIC_STICKER_CATALOG = buildCatalog();
export const P0A_STATIC_STICKER_COUNT = P0A_STATIC_STICKER_CATALOG.length;

export function stickerWindow(items: readonly StaticStickerCatalogItem[], scrollTop: number, viewportHeight: number): VirtualStickerWindow<StaticStickerCatalogItem> {
  return getVirtualStickerWindow(items, { columns: 2, itemHeight: 144, overscanScreens: 2, scrollTop, viewportHeight });
}

export { staticStickerOriginalUrl, staticStickerThumbnailUrl };
export type { StaticStickerCatalogItem };
