"use strict";
const common_vendor = require("../vendor.js");
const gen_Apis = require("../../gen/Apis.js");
const common_libraries_apiLoading = require("../libraries/apiLoading.js");
const wxworkId = "ww96c806131a95c739";
const wxAppId = "wx04d2d27ca198367b";
const useWeAppAuthStore = common_vendor.defineStore("we_app_auth", () => {
  const loading = common_vendor.ref(false);
  const hasError = common_vendor.ref(false);
  const data = common_vendor.ref({
    user: { id: 0, name: "", phone: "" },
    work_info: { session_key: "", openid: "" },
    environment: ""
  });
  function login(app) {
    const res = common_vendor.index.getSystemInfoSync();
    data.value.environment = res == null ? void 0 : res.environment;
    console.log(res == null ? void 0 : res.environment, "res?.environment");
    if ((res == null ? void 0 : res.environment) === "wxwork") {
      common_vendor.index.qy.login({
        success: function(res2) {
          console.log(res2);
          getWorkToken(app, res2.code);
        }
      });
    } else {
      common_vendor.index.login({
        provider: "weixin",
        //使用微信登录
        success: function(loginRes) {
          console.log(loginRes);
          getWXToken(app, loginRes.code);
        }
      });
    }
  }
  const getWorkToken = (app, code) => {
    gen_Apis.Apis.Login.Auth.WorkLogin({ code, app_id: wxworkId }).then((res) => {
      var _a, _b, _c, _d;
      console.log("企业微信登录", res == null ? void 0 : res.data);
      data.value.user = (_a = res.data) == null ? void 0 : _a.user;
      data.value.work_info = (_b = res.data) == null ? void 0 : _b.work_info;
      loading.value = false;
      common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.token) == null ? void 0 : _d.token);
      app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
    }).catch(() => {
      loading.value = false;
      hasError.value = true;
    });
  };
  const getWXToken = (app, code) => {
    gen_Apis.Apis.Login.Auth.Login({ code, app_id: wxAppId }).then((res) => {
      var _a, _b, _c;
      console.log("登录", res == null ? void 0 : res.data);
      data.value.user = (_a = res.data) == null ? void 0 : _a.user;
      loading.value = false;
      common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b.token) == null ? void 0 : _c.token);
      app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
    }).catch(() => {
      loading.value = false;
      hasError.value = true;
    });
  };
  function me(fun) {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.Me, {}).then((res) => {
      data.value.user = res.data.user;
      console.log("me", res.data);
      loading.value = false;
      hasError.value = false;
      return fun();
    });
  }
  const handleUserLoginWxwork = async (from_data) => {
    var _a, _b;
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindWork, {
      app_id: wxAppId,
      session_key: (_b = (_a = data == null ? void 0 : data.value) == null ? void 0 : _a.work_info) == null ? void 0 : _b.session_key,
      iv: from_data == null ? void 0 : from_data.iv,
      encrypted_data: from_data == null ? void 0 : from_data.encryptedData
    }).then((res) => {
      me(() => {
        common_vendor.index.navigateBack({ delta: 1 });
      });
    });
  };
  const handleUserLogin = async (from_data) => {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindPhoneNumber, {
      app_id: wxAppId,
      ...from_data
    }).then((res) => {
      me(() => {
        common_vendor.index.navigateBack({ delta: 1 });
      });
    });
  };
  return {
    loading,
    hasError,
    data,
    login,
    me,
    handleUserLogin,
    handleUserLoginWxwork
  };
});
exports.useWeAppAuthStore = useWeAppAuthStore;
