/*  hooks order 
 1) extension
 2) mixins
 3) component
 */

import { mapState, mapActions } from "pinia";
// import HttpService from "~/services/httpService";
import { useCommonStore } from "~/stores/commonStore";

export default {
  // beforeMount() {
  //   this.searchHttpService = new HttpService(import.meta.env.VITE_REPO_BASE_URL);
  // },
  data() {
    return {
      searchHttpService: undefined,
      originNokeysArray: [],
      viewMode: "table",
      listItem: [],

      sorting: {
        sortby: "created",
        sortorder: "asc", // asc | desc
      },

      filterUrl: "",
      countInPage: 10,
      page: 0,

      pagination: {
        pages: 0,
        total: 0,
        page: 1,
        offset: 0, // page * per_page
        limit: 10, //per_page
      },

      fetchingData: false,
      tableColumns: [
        {
          key: "key",
          title: "عنوان",
          width: "2",
        },
        {
          key: "doc_count",
          title: "تعداد تکرار",
          width: "2",
        },
        {
          key: "last_access",
          title: "تاریخ",
          width: "4",
        },
      ],
    };
  },
  computed: {
    ...mapState(useCommonStore, ["getPanelStatus", "isSidebarCollapsed"]),
    listTitle() {
      return this.$route.meta.breadcrumb;
    },
  },
  methods: {
    ...mapActions(useCommonStore, ["TOGGLE_PANEL"]),
    toggleSidebarMenu() {
      const CommonStore = useCommonStore();
      CommonStore.TOGGLE_SIDEBAR_MENU();
    },
    filterUpdate: function (filter) {
      this.filterUrl = filter;
      this.resetPagination();
      this.getData();
    },
    async getData(apiKey = this.$route.meta.apiKey, limit = undefined) {
      // if(this.fetchingData) return;
      // this.fetchingData = true;
      var vm = this;
      if (limit === undefined) limit = vm.pagination.limit;
      // this.fetchingData = true;
      var suburl = `/searchlog/`;
      // searchlog/nokeys/0/10

      try {
        const { $api } = useNuxtApp();
        const response = await $api(
          suburl +
            apiKey +
            "/" +
            vm.pagination.offset +
            "/" +
            limit +
            "?sortby=" +
            vm.sorting.sortby +
            "&sortorder=" +
            vm.sorting.sortorder +
            vm.filterUrl,
          {
            baseURL: import.meta.env.VITE_REPO_BASE_URL,
            method: "GET",
          }
        );

        vm.listItem = [];
        vm.fetchingData = false;

        if (response?.hits) {
          vm.pagination.pages = Math.ceil(
            response.hits.total.value / vm.pagination.limit
          );
          vm.pagination.total = response.hits.total.value;
          vm.listItem = response.aggregations.query_String.buckets;
        } else {
          this.originNokeysArray = structuredClone(response.buckets);

          vm.pagination.pages = Math.ceil(
            response.buckets.length / vm.pagination.limit
          );
          vm.pagination.total = response.buckets.length;

          vm.listItem = response.buckets.slice(
            vm.pagination.offset,
            vm.pagination.limit
          );
        }
        setTimeout(() => {
          vm.$refs?.filterlist?.setAnswer(response.aggregations);
        }, 500);

        return vm.listItem;
      } catch (err) {
        
      }
    },
    resetPagination() {
      this.page = 0;
      this.countInPage = 10;

      this.pagination = {
        pages: 0,
        total: 0,
        page: 1,
        offset: 0, // page * per_page
        limit: 10, //per_page
      };
    },
    pageChanged(paging) {
      // keep original page number and just updating the offset.
      let page = paging.pageNumber;
      page -= 1;
      this.pagination.offset = page * paging.limit;
      this.pagination.limit = paging.limit;
      this.pagination.page = paging.pageNumber;

      this.getData();
    },
    pageLimitChanged(paging) {
      this.resetPagination();
      this.pagination.limit = paging.limit;

      this.getData();
    },
    sortChanged(sorting) {
      // keep limit status.
      // reset page and offset values.
      this.pagination.page = this.pagination.offset = 0;
      this.sorting = sorting;

      this.getData();
    },
  },
};
