import type shopListQuery from './shopQuery'
import { type warehouseListQuery, type listingQuery, type listingListQuery, type shopOrderListQuery, type shopOrderDetailQuery,type bindShopQuery,type listedProductsQuery,type syncListedProductStockQuery,type matchWarehouseQuery,type shopOrderCancelQuery } from './shopQuery'
import type shopList from '~/domain/Shop'
import { type warehouseList, type shopOrder, type shopOrderDetailType,type listedProductsType } from '~/domain/Shop'
import type { ServerList, ServerRes, ServerListData } from '~/service/index'
import httpRequest from "~/service/index";

export function useShop() {
  const apiRoot = '/seller/sellerShop'

  // 获取授权店铺列表
  async function sellerShopList(query: shopListQuery) {
    return await httpRequest.post<ServerList<shopList>>(`${apiRoot}/list`, query)
  }

  // 获取授权链接
  async function createAuthorizationLink(query: bindShopQuery) {
    return await httpRequest.post<any>(`${apiRoot}/createAuthorizationLink`, query)
  }

  // 删除店铺/解除授权
  async function removeShop(id: number) {
    return await httpRequest.post<ServerList<any>>(`${apiRoot}/remove/${id}`, {})
  }

  // 获取店铺仓库列表
  async function shopWarehouseList(query: warehouseListQuery) {
    return await httpRequest.post<ServerListData<warehouseList>>(`/seller/shopWarehouse/list`, query)
  }

  // 刊登产品
  async function spuListing(id: number, query: listingQuery) {
    return await httpRequest.post<ServerList<warehouseList>>(`/seller/spu/listing/${id}`, query)
  }

  // 获取刊登记录
  async function shopListingList(query: listingListQuery) {
    return await httpRequest.post<ServerList<any>>(`/seller/shopListing/list`, query)
  }

  // 获取店铺订单列表
  async function shopOrderList(query: shopOrderListQuery) {
    return await httpRequest.post<ServerList<shopOrder>>(`/seller/shopOrder/list`, query)
  }

  // 获取店铺订单明细
  async function shopOrderDetail(query: shopOrderDetailQuery) {
    return await httpRequest.post<ServerList<shopOrderDetailType>>(`/seller/shopOrderDetail/list`, query)
  }

  // 获取已刊登产品列表
  async function listedProducts(query: listedProductsQuery) {
    return await httpRequest.post<ServerList<listedProductsType>>(`${apiRoot}/listedProducts`, query)
  }

  // 库存同步
  async function syncListedProductStock(query: syncListedProductStockQuery) {
    return await httpRequest.post<any>(`${apiRoot}/syncListedProductStock`, query)
  }

  // 取消产品库存同步
  async function disableSyncStock(query: syncListedProductStockQuery) {
    return await httpRequest.post<any>(`${apiRoot}/disableSyncStock`, query)
  }

  // 开启产品库存同步
  async function enableSyncStock(query: syncListedProductStockQuery) {
    return await httpRequest.post<any>(`${apiRoot}/enableSyncStock`, query)
  }

  // 店铺订单匹配仓库/运输方式
  async function matchWarehouse(query: matchWarehouseQuery) {
    return await httpRequest.post<any>(`/seller/shopOrder/matchWarehouse`, query)
  }

  // 查询店铺订单的运输方式列表
  async function listShippingProducts(query: {shopOrderId:string}) {
    return await httpRequest.post<any>(`/seller/shopOrder/listShippingProducts`, query)
  }

  // 更新店铺订单的运输方式
  async function shippingUpdate(query: {shopOrderId:string,shippingProductCode:string}) {
    return await httpRequest.post<any>(`/seller/shopOrder/update`, query)
  }

  // 店铺订单提交下单
  async function submitShopOrders(query: matchWarehouseQuery) {
    return await httpRequest.post<any>(`/seller/shopOrder/submitShopOrders`, query)
  }

  // 取消店铺订单
  async function shopOrderCancel(query: shopOrderCancelQuery) {
    return await httpRequest.post<any>(`/seller/shopOrder/cancel`, query)
  }


  return {
    sellerShopList,
    createAuthorizationLink,
    removeShop,
    shopWarehouseList,
    spuListing,
    shopListingList,
    shopOrderList,
    shopOrderDetail,
    listedProducts,
    syncListedProductStock,
    disableSyncStock,
    enableSyncStock,
    matchWarehouse,
    listShippingProducts,
    shippingUpdate,
    submitShopOrders,
    shopOrderCancel
  }
}
