export const FONT_RESOURCE_TYPE_ID = "019c4794-c53d-711d-8e2a-aec308a644b2";

export const RESOURCE_LIST_STYLE_PREFIX = "resource:";

export interface ListBulletResource {
  id: string;
  name: { en?: string; am?: string };
  presigned: string;
  resourceTypeId: string;
  fileInfo?: {
    bucket?: string;
    fileName?: string;
    contentType?: string;
    size?: number;
    originalname?: string;
  };
}

const LIST_BULLET_CODE_PATTERNS = [
  /^bullet/i,
  /^bullets/i,
  /^list[-_]?bullet/i,
  /^list[-_]?style/i,
  /^list[-_]?icon/i,
  /^icon/i,
  /^image/i,
];

export function isFontResourceType(type: {
  id: string;
  code?: string;
}): boolean {
  return (
    type.id === FONT_RESOURCE_TYPE_ID || /font/i.test(type.code || "")
  );
}

export function isListBulletResourceType(type: {
  id: string;
  code?: string;
  name?: string | { en?: string; am?: string };
}): boolean {
  if (isFontResourceType(type)) return false;

  const code = (type.code || "").toLowerCase();
  if (LIST_BULLET_CODE_PATTERNS.some((pattern) => pattern.test(code))) {
    return true;
  }

  const rawName = type.name;
  const nameEn =
    typeof rawName === "string"
      ? rawName.toLowerCase()
      : (rawName?.en || "").toLowerCase();
  const nameAm =
    typeof rawName === "string"
      ? rawName.toLowerCase()
      : (rawName?.am || "").toLowerCase();
  return (
    nameEn.includes("bullet") ||
    nameAm.includes("bullet") ||
    nameEn.includes("list style") ||
    nameEn.includes("list-style")
  );
}

export function isResourceListStyle(value: string): boolean {
  return value.startsWith(RESOURCE_LIST_STYLE_PREFIX);
}

export function toResourceListStyleValue(resourceId: string): string {
  return `${RESOURCE_LIST_STYLE_PREFIX}${resourceId}`;
}

export function isCustomListStyleImage(value?: string): boolean {
  if (!value || !value.startsWith("url(")) return false;
  return !value.includes("data:image/svg+xml");
}

export function extractUrlFromListStyleImage(value?: string): string | null {
  if (!value?.startsWith("url(")) return null;
  const match = value.match(/^url\(\s*['"]?([^'")]+)['"]?\s*\)$/i);
  return match?.[1] ?? null;
}

export function isDisclosureListStyleImage(value?: string): boolean {
  return Boolean(value?.includes("data:image/svg+xml"));
}
