"use strict";
const common_vendor = require("../common/vendor.js");
const BASE_URL = "https://api.makesong.cn/yu";
function request(url, options = {}) {
  const token = common_vendor.index.getStorageSync("token");
  let requestUrl = BASE_URL + url;
  if ((options.method || "GET") === "GET" && options.data) {
    const params = [];
    Object.keys(options.data).forEach((key) => {
      const value = options.data[key];
      if (value !== void 0 && value !== null) {
        params.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
      }
    });
    const queryString = params.join("&");
    if (queryString) {
      requestUrl += "?" + queryString;
    }
  }
  return new Promise((resolve, reject) => {
    common_vendor.index.request({
      url: requestUrl,
      method: options.method || "GET",
      data: (options.method || "GET") === "GET" ? void 0 : options.data,
      header: {
        "Content-Type": "application/json",
        ...token && { Authorization: `Bearer ${token}` },
        ...options.header
      },
      success: (res) => {
        const data = res.data;
        if (data.code === 200) {
          resolve(data.data);
        } else if (data.code == 401) {
          common_vendor.index.removeStorageSync("token");
          common_vendor.index.reLaunch({
            url: "/pages/login/login"
          });
        } else {
          common_vendor.index.showToast({
            title: data.message || "请求失败",
            icon: "none"
          });
          reject(new Error(data.message));
        }
      },
      fail: (err) => {
        common_vendor.index.showToast({
          title: "网络请求失败",
          icon: "none"
        });
        reject(err);
      }
    });
  });
}
const api = {
  // 认证相关
  auth: {
    register: (phone, nickname) => request("/api/auth/register", {
      method: "POST",
      data: { phone, nickname }
    }),
    sendCode: (phone) => request("/api/auth/send-code", {
      method: "POST",
      data: { phone }
    }),
    verificationLogin: (phone, code) => request("/api/auth/verification-login", {
      method: "POST",
      data: { phone, code }
    }),
    oneClickLogin: (phone) => request("/api/auth/one-click-login", {
      method: "POST",
      data: { phone }
    })
  },
  // 用户相关
  user: {
    getProfile: () => request("/api/users/profile"),
    updateProfile: (data) => request("/api/users/profile", {
      method: "PUT",
      data
    })
  },
  // 时间槽相关
  timeslots: {
    getList: (params) => request("/api/timeslots", {
      data: params
    }),
    getById: (id) => request(`/api/timeslots/${id}`)
  },
  // 预约相关
  appointments: {
    getList: (params) => request("/api/appointments", {
      data: params
    }),
    getById: (id) => request(`/api/appointments/${id}`),
    create: (time_slot_id, people_count = 1, notes) => request("/api/appointments", {
      method: "POST",
      data: { time_slot_id, people_count, notes }
    }),
    cancel: (id) => request(`/api/appointments/${id}`, {
      method: "DELETE"
    })
  },
  // 健康检查
  health: () => request("/health")
};
exports.api = api;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/api.js.map
