"use strict";
const common_vendor = require("../vendor.js");
const gen_Apis = require("../../gen/Apis.js");
const common_libraries_setTabBar = require("../libraries/setTabBar.js");
const common_libraries_apiLoading = require("../libraries/apiLoading.js");
const wxAppId = "wx31500e871924b903";
const useWeAppAuthStore = common_vendor.defineStore("we_app_auth", () => {
  const loading = common_vendor.ref(false);
  const hasError = common_vendor.ref(false);
  const is_house_exist = common_vendor.ref(0);
  const data = common_vendor.ref({
    user: { id: 0, name: "" },
    work_info: { session_key: "", openid: "" },
    selected_house: {},
    environment: "",
    house_occupant: false,
    house_register: false,
    is_house_exist: 0,
    config: {}
  });
  function login(app) {
    common_vendor.index.login({
      provider: "weixin",
      //使用微信登录
      success: function(loginRes) {
        console.log(loginRes);
        getWXToken(app, loginRes.code);
      }
    });
  }
  const getWXToken = (app, code) => {
    gen_Apis.Apis.Login.Auth.Login({ code, app_id: wxAppId }).then((res) => {
      var _a, _b, _c, _d, _e, _f;
      console.log("登录", res == null ? void 0 : res.data);
      data.value.user = (_a = res.data) == null ? void 0 : _a.user;
      data.value.selected_house = (_b = res.data) == null ? void 0 : _b.selected_house;
      data.value.config = (_c = res.data) == null ? void 0 : _c.config;
      common_libraries_setTabBar.setTabBar((_d = res == null ? void 0 : res.data) == null ? void 0 : _d.config);
      common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e.token) == null ? void 0 : _f.token);
      if (data.value.user) {
        getIsHouseExist();
      }
      setTimeout(() => {
        loading.value = false;
        hasError.value = false;
      }, 300);
      app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
    }).catch(() => {
      loading.value = false;
      hasError.value = true;
    });
  };
  const getIsHouseExist = () => {
    gen_Apis.Apis.Archive.HouseOccupants.GetCustomerHouseIsExist().then((res) => {
      var _a, _b;
      data.value.house_occupant = ((_a = res.data) == null ? void 0 : _a.house_occupant) || false;
      data.value.house_register = ((_b = res.data) == null ? void 0 : _b.house_register) || false;
      data.value.is_house_exist = is_house_exist.value + 1;
    });
  };
  function me(fun) {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.Me, {}).then((res) => {
      var _a, _b, _c;
      data.value.user = res.data.user;
      data.value.config = (_a = res.data) == null ? void 0 : _a.config;
      data.value.selected_house = (_b = res.data) == null ? void 0 : _b.selected_house;
      common_libraries_setTabBar.setTabBar((_c = res == null ? void 0 : res.data) == null ? void 0 : _c.config);
      console.log("me", res.data);
      if (data.value.user) {
        getIsHouseExist();
      }
      setTimeout(() => {
        loading.value = false;
        hasError.value = false;
      }, 300);
      return fun == null ? void 0 : fun();
    });
  }
  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,
    getIsHouseExist
  };
});
exports.useWeAppAuthStore = useWeAppAuthStore;
