import { mapState } from "pinia";
import HttpService from "~/services/httpService";

export const detailsMixin = {
  mounted() {
    this.httpService = new HttpService(
      import.meta.env.VITE_REPO_BASE_URL + this.$route.meta.slug
    );
  },

  data() {
    return {
      httpService: undefined,
      flickityOptions: {
        fullscreen: true,
        wrapAround: false,
        initialIndex: 0,
        prevNextButtons: true,
        pageDots: false,
        // contain: true,
        cellAlign: "right",
        rightToLeft: true,
        imagesLoaded: true,
        lazyLoad: true,
        groupCells: 1,
        fade: true,
        arrowShape: {
          x0: 20,
          x1: 55,
          y1: 30,
          x2: 60,
          y2: 25,
          x3: 30,
        },
        // fullscreen: true
        // autoPlay: 5000

        // any options from Flickity can be used
      },
    };
  },

  computed: {
    ...mapState("jahat", ["files"]),
    ...mapState("common", ["mediaAssetsUrl", "mediaBaseUrlGetter"]),
    entityType() {
      return this.getEntityName(this.entity?.type_id);
    },
    editCount() {
      return this.entity.user_log ? this.entity.user_log.length : 0;
    },
    user_editCount() {
      if (this.entity.user_log) {
        // return 3;

        let obj = this.entity.user_log;
        const arr = Array.from(Object.keys(obj), (k) => obj[k]);

        const newSet = new Set();
        arr.forEach((item) => {
          newSet.add(item.user_id);
        });
        return newSet.size;

        // commented for later uses.
        // const users = this.groupBy(arr, "user_id");
      }
      return 0;
    },
  },
  methods: {
    // getEntityName(type_id) {
    //   if (!type_id) return "نامشخص";
    //   if (type_id == 1) return "مساله";
    //   else if (type_id == 2) return "پاسخ";
    //   else if (type_id == 3) return "معیار";

    //   return "نامعین";
    // },
    scroll(id, openModal) {
      document.getElementById(id).scrollIntoView({
        behavior: "smooth",
        block: "center",
      });
    },
    groupBy(arrayToGroup, key) {
      return arrayToGroup.reduce(function (previousValue, currentValue) {
        (previousValue[currentValue[key]] =
          previousValue[currentValue[key]] || []).push({
          property: currentValue.property,
          time_edit: currentValue.time_edit,
          user_id: currentValue.user_id,
          username: currentValue.username,
        });
        return previousValue;
      }, {});
    },
  },
};