"use strict";
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
const common_vendor = require("../../common/vendor.js");
const sheep_api_trade_cart = require("../api/trade/cart.js");
const cart = common_vendor.defineStore({
  id: "cart",
  state: () => ({
    list: [],
    // 购物车列表
    selectedIds: [],
    // 已选列表
    isAllSelected: false,
    // 是否全选
    totalPriceSelected: 0
    // 选中项总金额
  }),
  actions: {
    // 获取购物车列表
    getList() {
      return __async(this, null, function* () {
        const { data, code } = yield sheep_api_trade_cart.CartApi.getCartList();
        if (code === 0) {
          this.list = data.validList;
          this.selectedIds = [];
          this.isAllSelected = true;
          this.totalPriceSelected = 0;
          this.list.forEach((item) => {
            if (item.selected) {
              this.selectedIds.push(item.id);
              this.totalPriceSelected += item.count * item.sku.price;
            } else {
              this.isAllSelected = false;
            }
          });
        }
      });
    },
    // 添加购物车
    add(goodsInfo) {
      return __async(this, null, function* () {
        const { code } = yield sheep_api_trade_cart.CartApi.addCart({
          skuId: goodsInfo.id,
          count: goodsInfo.goods_num
        });
        if (code === 0) {
          yield this.getList();
        }
      });
    },
    // 更新购物车
    update(goodsInfo) {
      return __async(this, null, function* () {
        const { code } = yield sheep_api_trade_cart.CartApi.updateCartCount({
          id: goodsInfo.goods_id,
          count: goodsInfo.goods_num
        });
        if (code === 0) {
          yield this.getList();
        }
      });
    },
    // 移除购物车
    delete(ids) {
      return __async(this, null, function* () {
        let idsTemp = "";
        if (Array.isArray(ids)) {
          idsTemp = ids.join(",");
        } else {
          idsTemp = ids;
        }
        const { code } = yield sheep_api_trade_cart.CartApi.deleteCart(idsTemp);
        if (code === 0) {
          yield this.getList();
        }
      });
    },
    // 单选购物车商品
    selectSingle(goodsId) {
      return __async(this, null, function* () {
        const { code } = yield sheep_api_trade_cart.CartApi.updateCartSelected({
          ids: [goodsId],
          selected: !this.selectedIds.includes(goodsId)
          // 取反
        });
        if (code === 0) {
          yield this.getList();
        }
      });
    },
    // 全选购物车商品
    selectAll(flag) {
      return __async(this, null, function* () {
        const { code } = yield sheep_api_trade_cart.CartApi.updateCartSelected({
          ids: this.list.map((item) => item.id),
          selected: flag
        });
        if (code === 0) {
          yield this.getList();
        }
      });
    },
    // 清空购物车。注意,仅用于用户退出时,重置数据
    emptyList() {
      this.list = [];
      this.selectedIds = [];
      this.isAllSelected = true;
      this.totalPriceSelected = 0;
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: "cart-store"
      }
    ]
  }
});
const __vite_glob_0_1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  default: cart
}, Symbol.toStringTag, { value: "Module" }));
exports.__vite_glob_0_1 = __vite_glob_0_1;
exports.cart = cart;
//# sourceMappingURL=cart.js.map