const MISHKA_CUSTOM = { prefix: 'custom:', legacyCursorId: 'custom', legacyBackgroundId: 'custom', cursorId: 'custom', backgroundId: 'custom' };
const MISHKA_LOCAL_KEYS = ['customCursors', 'customBackgrounds', 'customCursor', 'customBackground'];
const MISHKA_MAX_SAVED_CURSORS = 12;
const MISHKA_MAX_SAVED_BACKGROUNDS = 12;
const MISHKA_CURSOR_CATEGORIES = ['all', 'cute', 'animated'];
const MISHKA_BG_CATEGORIES = ['all', 'anime', 'pickme'];

const MISHKA_THEMES = {
  cursors: {
      "none": {
          "id": "none",
          "name": "Default",
          "emoji": "🖱️",
          "category": "all",
          "animated": false,
          "file": "",
          "hotspot": [
              0,
              0
          ]
      },
      "pink": {
          "id": "pink",
          "name": "Pink",
          "emoji": "💗",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/pink.png",
          "hotspot": [
              2,
              2
          ]
      },
      "cat": {
          "id": "cat",
          "name": "Cat",
          "emoji": "🐱",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/cat.png",
          "hotspot": [
              2,
              2
          ]
      },
      "heart": {
          "id": "heart",
          "name": "Heart",
          "emoji": "💖",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/heart.png",
          "hotspot": [
              2,
              2
          ]
      },
      "star": {
          "id": "star",
          "name": "Star",
          "emoji": "⭐",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/star.png",
          "hotspot": [
              2,
              2
          ]
      },
      "paw": {
          "id": "paw",
          "name": "Paw",
          "emoji": "🐾",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/paw.png",
          "hotspot": [
              2,
              2
          ]
      },
      "bunny": {
          "id": "bunny",
          "name": "Bunny",
          "emoji": "🐰",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/bunny.png",
          "hotspot": [
              2,
              2
          ]
      },
      "bear": {
          "id": "bear",
          "name": "Bear",
          "emoji": "🧸",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/bear.png",
          "hotspot": [
              2,
              2
          ]
      },
      "sparkle": {
          "id": "sparkle",
          "name": "Sparkle",
          "emoji": "✨",
          "category": "cute",
          "animated": false,
          "file": "assets/cursors/sparkle.png",
          "hotspot": [
              2,
              2
          ]
      },
      "cat-wiggle": {
          "id": "cat-wiggle",
          "name": "Wiggle Cat",
          "emoji": "🐱",
          "category": "animated",
          "animated": true,
          "file": "assets/cursors/cat-wiggle.gif",
          "hotspot": [
              2,
              2
          ]
      },
      "heart-pulse": {
          "id": "heart-pulse",
          "name": "Pulse Heart",
          "emoji": "💓",
          "category": "animated",
          "animated": true,
          "file": "assets/cursors/heart-pulse.gif",
          "hotspot": [
              2,
              2
          ]
      },
      "sparkle-spin": {
          "id": "sparkle-spin",
          "name": "Spin Sparkle",
          "emoji": "✨",
          "category": "animated",
          "animated": true,
          "file": "assets/cursors/sparkle-spin.gif",
          "hotspot": [
              2,
              2
          ]
      },
      "paw-bounce": {
          "id": "paw-bounce",
          "name": "Bounce Paw",
          "emoji": "🐾",
          "category": "animated",
          "animated": true,
          "file": "assets/cursors/paw-bounce.gif",
          "hotspot": [
              2,
              2
          ]
      }
  },
  backgrounds: {
      "none": {
          "id": "none",
          "name": "Roblox Default",
          "emoji": "✨",
          "type": "css",
          "css": "",
          "category": "all"
      },
      "photo-anime-flowers": {
          "id": "photo-anime-flowers",
          "name": "🌸 Flower Field",
          "emoji": "🌸",
          "type": "image",
          "file": "assets/backgrounds/anime-flowers.jpg",
          "category": "anime"
      },
      "photo-anime-wind": {
          "id": "photo-anime-wind",
          "name": "🎀 Soft Wind",
          "emoji": "🎀",
          "type": "image",
          "file": "assets/backgrounds/anime-wind.jpg",
          "category": "anime"
      },
      "photo-anime-library": {
          "id": "photo-anime-library",
          "name": "📚 Library",
          "emoji": "📚",
          "type": "image",
          "file": "assets/backgrounds/anime-library.jpg",
          "category": "anime"
      },
      "photo-anime-portrait": {
          "id": "photo-anime-portrait",
          "name": "✨ Portrait",
          "emoji": "✨",
          "type": "image",
          "file": "assets/backgrounds/anime-portrait.jpg",
          "category": "anime"
      },
      "photo-anime-dream": {
          "id": "photo-anime-dream",
          "name": "🌙 Dreamy",
          "emoji": "🌙",
          "type": "image",
          "file": "assets/backgrounds/anime-dream.jpg",
          "category": "anime"
      },
      "photo-anime-mood": {
          "id": "photo-anime-mood",
          "name": "💕 Mood",
          "emoji": "💕",
          "type": "image",
          "file": "assets/backgrounds/anime-mood.jpg",
          "category": "pickme"
      },
      "photo-anime-night": {
          "id": "photo-anime-night",
          "name": "🌃 Night",
          "emoji": "🌃",
          "type": "image",
          "file": "assets/backgrounds/anime-night.jpg",
          "category": "anime"
      },
      "photo-anime-soft": {
          "id": "photo-anime-soft",
          "name": "🌸 Soft",
          "emoji": "🌸",
          "type": "image",
          "file": "assets/backgrounds/anime-soft.jpg",
          "category": "pickme"
      },
      "photo-anime-moon": {
          "id": "photo-anime-moon",
          "name": "🌙 Moon Balcony",
          "emoji": "🌙",
          "type": "image",
          "file": "assets/backgrounds/anime-moon.jpg",
          "category": "anime"
      }
  },
};

const MISHKA_DEFAULTS = {
  enabled: false,
  cursorId: 'none',
  backgroundId: 'none',
  customBgMode: 'cover',
};

function mishkaParseCustomEntryId(selectionId) {
  if (!selectionId || typeof selectionId !== 'string') return null;
  if (selectionId.startsWith(MISHKA_CUSTOM.prefix)) return selectionId.slice(MISHKA_CUSTOM.prefix.length);
  if (selectionId === MISHKA_CUSTOM.legacyCursorId || selectionId === MISHKA_CUSTOM.legacyBackgroundId) return '__legacy__';
  return null;
}

function mishkaCustomEntryId(id) {
  return MISHKA_CUSTOM.prefix + id;
}

function mishkaFindCustomCursor(settings) {
  const key = mishkaParseCustomEntryId(settings?.cursorId);
  if (!key) return null;
  if (key === '__legacy__') return settings.customCursor || null;
  return (settings.customCursors || []).find((c) => c.id === key) || null;
}

function mishkaFindCustomBackground(settings) {
  const key = mishkaParseCustomEntryId(settings?.backgroundId);
  if (!key) return null;
  if (key === '__legacy__') return settings.customBackground || null;
  return (settings.customBackgrounds || []).find((b) => b.id === key) || null;
}

function mishkaIsCustomCursorSelected(cursorId) {
  return Boolean(mishkaParseCustomEntryId(cursorId));
}

function mishkaIsCustomBackgroundSelected(backgroundId) {
  return Boolean(mishkaParseCustomEntryId(backgroundId));
}

if (typeof module !== 'undefined') module.exports = { MISHKA_THEMES, MISHKA_DEFAULTS, MISHKA_CUSTOM, MISHKA_LOCAL_KEYS, MISHKA_CURSOR_CATEGORIES, MISHKA_BG_CATEGORIES, mishkaParseCustomEntryId, mishkaCustomEntryId, mishkaFindCustomCursor, mishkaFindCustomBackground, mishkaIsCustomCursorSelected, mishkaIsCustomBackgroundSelected };
