'use strict';

var React2 = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React2__default = /*#__PURE__*/_interopDefault(React2);

// 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)
  };
};
var resizeFestivalCardPages = (config, pageCount) => {
  const safeCount = Math.max(1, Math.min(12, Math.floor(pageCount || 1)));
  const nextPages = [...config.pages];
  while (nextPages.length < safeCount) {
    const idx = nextPages.length;
    nextPages.push({
      id: `page-${idx + 1}`,
      title: `\u7B2C ${idx + 1} \u9875`,
      elements: [],
      background: {}
    });
  }
  return {
    ...config,
    pages: nextPages.slice(0, safeCount)
  };
};
var useFestivalCardConfig = (options) => {
  const [config, setConfig] = React2.useState(
    () => normalizeFestivalCardConfig(options?.initialConfig || DEFAULT_FESTIVAL_CARD_CONFIG)
  );
  const [loading, setLoading] = React2.useState(Boolean(options?.fetchConfig));
  const [saving, setSaving] = React2.useState(false);
  React2.useEffect(() => {
    const fetchConfig = options?.fetchConfig;
    if (!fetchConfig) return;
    let active = true;
    void fetchConfig().then((value) => {
      if (!active) return;
      setConfig(normalizeFestivalCardConfig(value));
    }).finally(() => {
      if (active) setLoading(false);
    });
    return () => {
      active = false;
    };
  }, [options?.fetchConfig]);
  const save = React2.useCallback(async () => {
    const onSave = options?.onSave;
    if (!onSave) return;
    setSaving(true);
    try {
      await onSave(config);
    } finally {
      setSaving(false);
    }
  }, [config, options?.onSave]);
  return React2.useMemo(
    () => ({
      config,
      setConfig,
      loading,
      saving,
      save
    }),
    [config, loading, save, saving]
  );
};
var elementStyle = (element) => ({
  position: "absolute",
  zIndex: 2,
  left: `${element.x}%`,
  top: `${element.y}%`,
  width: `${element.width ?? 70}%`,
  height: element.height ? `${element.height}%` : void 0,
  transform: "translate(-50%, -50%)"
});
var renderElement = (element) => {
  if (element.type === "text") {
    return /* @__PURE__ */ React2__default.default.createElement(
      "div",
      {
        key: element.id,
        style: {
          ...elementStyle(element),
          color: element.color || "#f8fafc",
          fontSize: element.fontSize || 18,
          fontWeight: element.fontWeight || 500,
          textAlign: element.align || "left",
          lineHeight: 1.45,
          whiteSpace: "pre-wrap"
        }
      },
      element.content
    );
  }
  return /* @__PURE__ */ React2__default.default.createElement(
    "img",
    {
      key: element.id,
      src: element.src,
      alt: element.alt || "festival-card-image",
      style: {
        ...elementStyle(element),
        objectFit: element.fit || "cover",
        borderRadius: element.borderRadius || 0,
        overflow: "hidden",
        boxShadow: "0 12px 30px rgba(2, 6, 23, 0.32)"
      }
    }
  );
};
var FestivalCardPageRenderer = ({ page }) => {
  const backgroundElement = page.elements.find(
    (element) => element.type === "image" && Boolean(element.isBackground)
  );
  const foregroundElements = page.elements.filter((element) => !(element.type === "image" && element.isBackground));
  return /* @__PURE__ */ React2__default.default.createElement(
    "div",
    {
      className: "relative h-full w-full overflow-hidden rounded-2xl",
      style: {
        backgroundColor: page.background?.color || "#0f172a",
        backgroundImage: backgroundElement ? `url(${backgroundElement.src})` : page.background?.image ? `url(${page.background.image})` : void 0,
        backgroundSize: "cover",
        backgroundPosition: "center"
      }
    },
    /* @__PURE__ */ React2__default.default.createElement("div", { className: "absolute inset-0 bg-slate-950/20" }),
    foregroundElements.map(renderElement)
  );
};

// src/festivalCard/components/FestivalCardMiniapp.tsx
var FestivalCardMiniapp = ({ config }) => {
  const normalized = React2.useMemo(() => normalizeFestivalCardConfig(config), [config]);
  const [index, setIndex] = React2.useState(0);
  const page = normalized.pages[index];
  if (!page) return null;
  return /* @__PURE__ */ React2__default.default.createElement("div", null, /* @__PURE__ */ React2__default.default.createElement("div", { style: { height: 420 } }, /* @__PURE__ */ React2__default.default.createElement(FestivalCardPageRenderer, { page })), /* @__PURE__ */ React2__default.default.createElement("div", { style: { display: "flex", justifyContent: "space-between", marginTop: 12 } }, /* @__PURE__ */ React2__default.default.createElement("button", { type: "button", disabled: index <= 0, onClick: () => setIndex((v) => Math.max(0, v - 1)) }, "\u4E0A\u4E00\u9875"), /* @__PURE__ */ React2__default.default.createElement("div", null, index + 1, "/", normalized.pages.length), /* @__PURE__ */ React2__default.default.createElement(
    "button",
    {
      type: "button",
      disabled: index >= normalized.pages.length - 1,
      onClick: () => setIndex((v) => Math.min(normalized.pages.length - 1, v + 1))
    },
    "\u4E0B\u4E00\u9875"
  )));
};

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

// 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);
}
var createInMemoryFestivalCardDb = () => ({
  listConfigs() {
    return Promise.resolve(
      Array.from(memoryStore.entries()).map(([id, config]) => ({
        id,
        name: config.name || id
      }))
    );
  },
  getConfig(id) {
    return Promise.resolve(memoryStore.get(id) || null);
  },
  saveConfig(id, config) {
    memoryStore.set(id, config);
    return Promise.resolve();
  },
  deleteConfig(id) {
    memoryStore.delete(id);
    return Promise.resolve();
  }
});

exports.DEFAULT_FESTIVAL_CARD_CONFIG = DEFAULT_FESTIVAL_CARD_CONFIG;
exports.FestivalCardMiniapp = FestivalCardMiniapp;
exports.FestivalCardService = FestivalCardService;
exports.createInMemoryFestivalCardDb = createInMemoryFestivalCardDb;
exports.normalizeFestivalCardConfig = normalizeFestivalCardConfig;
exports.resizeFestivalCardPages = resizeFestivalCardPages;
exports.useFestivalCardConfig = useFestivalCardConfig;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map