"use strict";
const common_vendor = require("../../common/vendor.js");
const sheep_store_index = require("../store/index.js");
const sheep_helper_index = require("../helper/index.js");
const sheep_helper_test = require("../helper/test.js");
const sheep_api_member_auth = require("../api/member/auth.js");
const sheep_index = require("../index.js");
const sheep_api_member_signin = require("../api/member/signin.js");
let time = 30;
let timer = null;
async function onSign() {
  console.log("onSign");
  const {
    code,
    data
  } = await sheep_api_member_signin.SignInApi.createSignInRecord();
  if (code === 0) {
    showSignUpModal(data);
  }
  if (timer) {
    clearInterval(timer);
  }
  common_vendor.index.setStorageSync("isRun", false);
  common_vendor.index.setStorageSync("isSign", true);
}
function resetSignStatusIfNeeded() {
  const today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
  const lastCheckDate = common_vendor.index.getStorageSync("lastCheckDate");
  if (today !== lastCheckDate) {
    common_vendor.index.setStorageSync("isSign", false);
    common_vendor.index.setStorageSync("lastCheckDate", today);
  }
}
function autoSign() {
  resetSignStatusIfNeeded();
  const isLogin = common_vendor.computed(() => sheep_index.sheep.$store("user").isLogin);
  if (isLogin.value) {
    common_vendor.index.setStorageSync("isRun", true);
    if (common_vendor.index.getStorageSync("isRun") && !common_vendor.index.getStorageSync("isSign")) {
      timer = setInterval(() => {
        time--;
        if (common_vendor.index.getStorageSync("isSign") || !isLogin.value) {
          cancelAutoSign();
        }
        if (time <= 0 && !common_vendor.index.getStorageSync("isSign")) {
          clearInterval(timer);
          onSign();
        }
      }, 1e3);
    }
  }
}
function cancelAutoSign() {
  if (timer) {
    clearInterval(timer);
  }
  time = 30;
  common_vendor.index.setStorageSync("isRun", false);
}
function showSignUpModal(obj) {
  sheep_store_index.$store("modal").$patch((state) => {
    state.signUp = true;
    state.signUpInfo = obj;
  });
}
function colseSignUpModal() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.signUp = false;
  });
}
function showWalletModal(obj) {
  sheep_store_index.$store("modal").$patch((state) => {
    state.getWallet = true;
    state.getWalletInfo = obj;
  });
}
function colseWalletModal() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.getWallet = false;
  });
}
function showAuthModal(type = "accountLogin", isActive = "accountLogin") {
  const modal = sheep_store_index.$store("modal");
  if (modal.auth !== "") {
    closeAuthModal();
    setTimeout(() => {
      modal.$patch((state) => {
        state.auth = type;
        state.isActive = isActive;
      });
    }, 100);
  } else {
    modal.$patch((state) => {
      state.auth = type;
      state.isActive = isActive;
    });
  }
}
function closeAuthModal() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.auth = "";
  });
}
function showShareModal(spuId = 0) {
  sheep_store_index.$store("modal").$patch((state) => {
    state.share = true;
    state.shareInfo.spuId = spuId;
  });
}
function closeShareModal() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.share = false;
  });
}
function showMenuTools() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.menu = true;
  });
}
function closeMenuTools() {
  sheep_store_index.$store("modal").$patch((state) => {
    state.menu = false;
  });
}
function getSmsCode(event, mobile) {
  const modalStore = sheep_store_index.$store("modal");
  const lastSendTimer = modalStore.lastTimer[event];
  console.log(lastSendTimer);
  if (typeof lastSendTimer === "undefined") {
    sheep_helper_index.$helper.toast("短信发送事件错误");
    return;
  }
  const duration = common_vendor.dayjs().unix() - lastSendTimer;
  const canSend = duration >= 60;
  if (!canSend) {
    sheep_helper_index.$helper.toast("请稍后再试");
    return;
  }
  if (mobile && !sheep_helper_test.test.mobile(mobile)) {
    sheep_helper_index.$helper.toast("手机号码格式不正确");
    return;
  }
  let scene = -1;
  switch (event) {
    case "resetPassword":
      scene = 4;
      break;
    case "changePassword":
      scene = 3;
      break;
    case "changeMobileOld":
      scene = 2;
      break;
    case "changeMobileNew":
      scene = 5;
      break;
    case "smsLogin":
      scene = 1;
      break;
  }
  sheep_api_member_auth.AuthUtil.sendSmsCode(mobile, scene).then((res) => {
    if (res.code === 0) {
      modalStore.$patch((state) => {
        state.lastTimer[event] = common_vendor.dayjs().unix();
      });
    }
  });
}
function getSmsTimer(event, mobile = "") {
  const modalStore = sheep_store_index.$store("modal");
  const lastSendTimer = modalStore.lastTimer[event];
  if (typeof lastSendTimer === "undefined") {
    sheep_helper_index.$helper.toast("短信发送事件错误");
    return;
  }
  const duration = common_vendor.ref(common_vendor.dayjs().unix() - lastSendTimer - 60);
  const canSend = duration.value >= 0;
  if (canSend) {
    return "获取验证码";
  }
  if (!canSend) {
    setTimeout(() => {
      duration.value++;
    }, 1e3);
    return -duration.value.toString() + " 秒";
  }
}
function saveAdvHistory(adv) {
  const modal = sheep_store_index.$store("modal");
  modal.$patch((state) => {
    if (!state.advHistory.includes(adv.imgUrl)) {
      state.advHistory.push(adv.imgUrl);
    }
  });
}
exports.autoSign = autoSign;
exports.cancelAutoSign = cancelAutoSign;
exports.closeAuthModal = closeAuthModal;
exports.closeMenuTools = closeMenuTools;
exports.closeShareModal = closeShareModal;
exports.colseSignUpModal = colseSignUpModal;
exports.colseWalletModal = colseWalletModal;
exports.getSmsCode = getSmsCode;
exports.getSmsTimer = getSmsTimer;
exports.resetSignStatusIfNeeded = resetSignStatusIfNeeded;
exports.saveAdvHistory = saveAdvHistory;
exports.showAuthModal = showAuthModal;
exports.showMenuTools = showMenuTools;
exports.showShareModal = showShareModal;
exports.showWalletModal = showWalletModal;
