import { reactive, watchEffect, toRefs } from "vue";
import { getShare } from "@/services/index";

interface ShareState {
  consume: number;
  defeat: number;
  duration: number;
  period: string;
  user: {};
  hash?: string;
  id?: number;
  type?: string;
  createdAt?: string;
}

export function initShare(hash: string) {
  // 把自己用到的再填上去 没用到的可以 不填...
  const state = reactive<ShareState>({
    user: {},
    consume: 0,
    defeat: 0,
    duration: 0,
    period: "",
    createdAt: ""
  });

  watchEffect(async function() {
    const res = await getShare(hash);
    const {
      data: { user, consume, duration, period },
      created_at
    } = res.data.data;

    state.user = user;
    state.consume = consume;
    state.duration = duration;
    state.period = period;
    state.createdAt = created_at.split(" ")[0];
    return state;
  });

  // console.log(state.data);

  return {
    ...toRefs(state)
  };
}
