import { NextResponse } from 'next/server';

// src/festivalCard/routes/index.ts

// src/festivalCard/server/db.ts
var dbAdapter = null;
var initializeFestivalCardDb = (adapter) => {
  dbAdapter = adapter;
};
var getFestivalCardDb = () => dbAdapter;

// src/festivalCard/core/defaults.ts
var DEFAULT_FESTIVAL_CARD_CONFIG = {
  id: "default-festival-card",
  name: "Holiday Card",
  theme: "winter",
  coverTitle: "Happy Holidays",
  coverSubtitle: "Warm wishes for you",
  background: {
    colorA: "#0c1a34",
    colorB: "#1f4f8a"
  },
  backgroundMusic: {
    src: "",
    loop: true,
    autoPlay: false,
    volume: 0.5
  },
  pages: [
    {
      id: "page-1",
      title: "\u5C01\u9762",
      background: { color: "#11284d" },
      elements: [
        {
          id: "p1-text-1",
          type: "text",
          x: 50,
          y: 20,
          content: "\u65B0\u5E74\u5FEB\u4E50",
          fontSize: 34,
          fontWeight: 700,
          align: "center",
          color: "#f8fafc"
        },
        {
          id: "p1-text-2",
          type: "text",
          x: 50,
          y: 36,
          content: "\u613F\u8FD9\u4E00\u5E74\u5E73\u5B89\u559C\u4E50",
          fontSize: 18,
          fontWeight: 500,
          align: "center",
          color: "#dbeafe"
        },
        {
          id: "p1-image-1",
          type: "image",
          x: 50,
          y: 68,
          width: 72,
          height: 42,
          src: "https://images.unsplash.com/photo-1512389142860-9c449e58a543?auto=format&fit=crop&w=1200&q=80",
          fit: "cover",
          borderRadius: 16,
          alt: "holiday",
          isBackground: false
        }
      ]
    },
    {
      id: "page-2",
      title: "\u795D\u798F",
      background: { color: "#1a3766" },
      elements: [
        {
          id: "p2-text-1",
          type: "text",
          x: 50,
          y: 28,
          content: "\u613F\u4F60\u65B0\u5C81\uFF1A",
          fontSize: 28,
          fontWeight: 700,
          align: "center",
          color: "#fef3c7"
        },
        {
          id: "p2-text-2",
          type: "text",
          x: 50,
          y: 42,
          content: "\u6240\u5F97\u7686\u6240\u671F\uFF0C\u6240\u5931\u4EA6\u65E0\u788D",
          fontSize: 18,
          fontWeight: 500,
          align: "center",
          color: "#f8fafc"
        },
        {
          id: "p2-text-3",
          type: "text",
          x: 50,
          y: 55,
          content: "\u613F\u4F60\u7684\u6BCF\u4E00\u6B65\u90FD\u8D70\u5411\u5149\u4EAE",
          fontSize: 16,
          fontWeight: 400,
          align: "center",
          color: "#dbeafe"
        }
      ]
    },
    {
      id: "page-3",
      title: "\u843D\u6B3E",
      background: { color: "#11284d" },
      elements: [
        {
          id: "p3-image-1",
          type: "image",
          x: 50,
          y: 34,
          width: 60,
          height: 42,
          src: "https://images.unsplash.com/photo-1456324504439-367cee3b3c32?auto=format&fit=crop&w=1200&q=80",
          fit: "cover",
          borderRadius: 14,
          alt: "gift",
          isBackground: false
        },
        {
          id: "p3-text-1",
          type: "text",
          x: 50,
          y: 72,
          content: "Best wishes, from SA2Kit",
          fontSize: 18,
          fontWeight: 600,
          align: "center",
          color: "#f8fafc"
        }
      ]
    }
  ]
};

// src/festivalCard/core/normalize.ts
var ensurePage = (page, index) => ({
  id: page.id || `page-${index + 1}`,
  title: page.title || `\u7B2C ${index + 1} \u9875`,
  elements: Array.isArray(page.elements) ? page.elements : [],
  background: page.background || {}
});
var normalizeFestivalCardConfig = (config) => {
  const pages = config?.pages && config.pages.length > 0 ? config.pages : DEFAULT_FESTIVAL_CARD_CONFIG.pages;
  return {
    ...DEFAULT_FESTIVAL_CARD_CONFIG,
    ...config,
    background: {
      ...DEFAULT_FESTIVAL_CARD_CONFIG.background,
      ...config?.background || {}
    },
    backgroundMusic: {
      ...DEFAULT_FESTIVAL_CARD_CONFIG.backgroundMusic,
      ...config?.backgroundMusic || {},
      src: config?.backgroundMusic?.src ?? DEFAULT_FESTIVAL_CARD_CONFIG.backgroundMusic?.src ?? ""
    },
    pages: pages.map(ensurePage)
  };
};

// src/festivalCard/services/festivalCardService.ts
var FestivalCardService = class {
  constructor(options) {
    this.db = options?.db || getFestivalCardDb();
  }
  async listConfigs() {
    if (!this.db) {
      return [{ id: DEFAULT_FESTIVAL_CARD_CONFIG.id || "default-festival-card", name: DEFAULT_FESTIVAL_CARD_CONFIG.name }];
    }
    if (this.db.listConfigs) {
      const list = await this.db.listConfigs();
      return list.length > 0 ? list : [{ id: DEFAULT_FESTIVAL_CARD_CONFIG.id || "default-festival-card", name: DEFAULT_FESTIVAL_CARD_CONFIG.name }];
    }
    return [{ id: DEFAULT_FESTIVAL_CARD_CONFIG.id || "default-festival-card", name: DEFAULT_FESTIVAL_CARD_CONFIG.name }];
  }
  async getConfig(cardId = "default-festival-card") {
    if (!this.db) return DEFAULT_FESTIVAL_CARD_CONFIG;
    const config = await this.db.getConfig(cardId);
    return normalizeFestivalCardConfig(config);
  }
  async saveConfig(cardId, config) {
    const normalized = normalizeFestivalCardConfig(config);
    if (!this.db) return normalized;
    await this.db.saveConfig(cardId, normalized);
    return normalized;
  }
  async deleteConfig(cardId) {
    if (!this.db?.deleteConfig) return;
    await this.db.deleteConfig(cardId);
  }
};
var memoryStore = /* @__PURE__ */ new Map();
var defaultId = DEFAULT_FESTIVAL_CARD_CONFIG.id || "default-festival-card";
if (!memoryStore.has(defaultId)) {
  memoryStore.set(defaultId, DEFAULT_FESTIVAL_CARD_CONFIG);
}

// src/festivalCard/routes/index.ts
var createService = (config) => {
  if (config.db) initializeFestivalCardDb(config.db);
  return new FestivalCardService({ db: config.db });
};
var resolveCardId = async (context) => {
  const params = await context.params;
  return params.id;
};
var createListFestivalCardsHandler = (config = {}) => {
  const service = createService(config);
  return async (_request) => {
    try {
      const data = await service.listConfigs();
      return NextResponse.json({ success: true, data });
    } catch (error) {
      return NextResponse.json({ success: false, error: error.message }, { status: 500 });
    }
  };
};
var createGetFestivalCardHandler = (config = {}) => {
  const service = createService(config);
  return async (_request, context) => {
    try {
      const cardId = await resolveCardId(context);
      const card = await service.getConfig(cardId);
      return NextResponse.json({ success: true, data: card });
    } catch (error) {
      return NextResponse.json({ success: false, error: error.message }, { status: 500 });
    }
  };
};
var createUpsertFestivalCardHandler = (config = {}) => {
  const service = createService(config);
  return async (request, context) => {
    try {
      const cardId = await resolveCardId(context);
      const body = await request.json();
      const payload = body && typeof body === "object" && "config" in body ? body.config : void 0;
      const normalized = normalizeFestivalCardConfig(payload || {});
      const data = await service.saveConfig(cardId, normalized);
      return NextResponse.json({ success: true, data });
    } catch (error) {
      return NextResponse.json({ success: false, error: error.message }, { status: 500 });
    }
  };
};
var createDeleteFestivalCardHandler = (config = {}) => {
  const service = createService(config);
  return async (_request, context) => {
    try {
      const cardId = await resolveCardId(context);
      await service.deleteConfig(cardId);
      return NextResponse.json({ success: true });
    } catch (error) {
      return NextResponse.json({ success: false, error: error.message }, { status: 500 });
    }
  };
};

export { createDeleteFestivalCardHandler, createGetFestivalCardHandler, createListFestivalCardsHandler, createUpsertFestivalCardHandler };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map