'use strict';

// src/showmasterpiece/logic/shared/category.ts
var CATEGORY_LABELS = {
  collection: "\u753B\u96C6",
  acrylic: "\u7ACB\u724C",
  badge: "\u5427\u5527",
  color_paper: "\u8272\u7EB8",
  transparent_card: "\u900F\u5361",
  postcard: "\u660E\u4FE1\u7247",
  laser_ticket: "\u956D\u5C04\u7968",
  canvas_bag: "\u5E06\u5E03\u5305",
  spiral_notebook: "\u7EBF\u5708\u672C",
  mouse_pad: "\u9F20\u6807\u57AB",
  support_stick: "\u5E94\u63F4\u68D2",
  keychain: "\u6302\u4EF6/\u94A5\u5319\u6263",
  other: "\u5176\u5B83"
};
var getCategoryLabel = (category) => {
  if (!category) return "\u672A\u5206\u7C7B";
  return CATEGORY_LABELS[String(category)] ?? String(category);
};

// src/showmasterpiece/logic/shared/format.ts
var formatPrice = (price) => {
  if (!price) return "\u4EF7\u683C\u5F85\u5B9A";
  return `\xA5${price}`;
};

// src/showmasterpiece/logic/shared/storage.ts
var hasWxStorage = () => typeof wx !== "undefined" && typeof wx.getStorageSync === "function";
var hasLocalStorage = () => typeof localStorage !== "undefined" && typeof localStorage.getItem === "function";
var getStorageSync = (key) => {
  try {
    if (hasWxStorage()) {
      return wx.getStorageSync(key);
    }
    if (hasLocalStorage()) {
      const value = localStorage.getItem(key);
      return value ? JSON.parse(value) : null;
    }
  } catch {
    return null;
  }
  return null;
};
var setStorageSync = (key, value) => {
  try {
    if (hasWxStorage()) {
      wx.setStorageSync(key, value);
      return;
    }
    if (hasLocalStorage()) {
      localStorage.setItem(key, JSON.stringify(value));
    }
  } catch {
  }
};

// src/showmasterpiece/logic/shared/cart.ts
var CART_STORAGE_KEY = "showmasterpiece_cart";
var normalizeCart = (cart) => {
  const safeCart = cart ?? { items: [], totalPrice: 0, totalQuantity: 0 };
  const totals = safeCart.items.reduce(
    (acc, item) => {
      acc.totalQuantity += item.quantity;
      acc.totalPrice += (item.collection.price || 0) * item.quantity;
      return acc;
    },
    { totalQuantity: 0, totalPrice: 0 }
  );
  return {
    ...safeCart,
    totalQuantity: totals.totalQuantity,
    totalPrice: totals.totalPrice
  };
};
var getCart = () => {
  const stored = getStorageSync(CART_STORAGE_KEY);
  if (stored && stored.items) {
    return normalizeCart(stored);
  }
  return normalizeCart();
};
var saveCart = (cart) => {
  setStorageSync(CART_STORAGE_KEY, cart);
};
var addToCart = (collection, quantity = 1) => {
  const cart = getCart();
  const existing = cart.items.find((item) => item.collectionId === collection.id);
  if (existing) {
    existing.quantity += quantity;
  } else {
    cart.items.push({
      collectionId: collection.id,
      collection,
      quantity,
      addedAt: /* @__PURE__ */ new Date()
    });
  }
  const next = normalizeCart(cart);
  saveCart(next);
  return next;
};
var updateCartItem = (collectionId, quantity) => {
  const cart = getCart();
  const target = cart.items.find((item) => item.collectionId === collectionId);
  if (!target) return cart;
  if (quantity <= 0) {
    cart.items = cart.items.filter((item) => item.collectionId !== collectionId);
  } else {
    target.quantity = quantity;
  }
  const next = normalizeCart(cart);
  saveCart(next);
  return next;
};
var removeCartItem = (collectionId) => {
  const cart = getCart();
  cart.items = cart.items.filter((item) => item.collectionId !== collectionId);
  const next = normalizeCart(cart);
  saveCart(next);
  return next;
};
var clearCart = () => {
  const empty = normalizeCart({ items: [], totalPrice: 0, totalQuantity: 0 });
  saveCart(empty);
  return empty;
};

exports.CATEGORY_LABELS = CATEGORY_LABELS;
exports.addToMiniappCart = addToCart;
exports.clearMiniappCart = clearCart;
exports.formatPrice = formatPrice;
exports.getCategoryLabel = getCategoryLabel;
exports.getMiniappCart = getCart;
exports.removeMiniappCartItem = removeCartItem;
exports.updateMiniappCartItem = updateCartItem;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map