import { computed, reactive } from "vue";
import { deepEqual, getCurrentInstance, propsFactory, sortItems, wrapInArray } from "@veno-ui/utils";
import { useProxiedModel } from "../proxied-model";
const defaultPagination = {
  page: 1,
  perPage: 10,
  perPageOptions: [10, 20, 50, 100],
  total: 0,
  totalVisible: 7,
  showQuickJumper: false,
  showPerPageSelect: false
};
const makeDataIteratorProps = propsFactory({
  remote: Boolean,
  items: {
    type: Array,
    default: () => []
  },
  mustSort: Boolean,
  multiSort: Boolean,
  sortBy: {
    type: [String, Array],
    default: () => []
  },
  sortDesc: {
    type: [Boolean, Array],
    default: () => []
  },
  customSort: {
    type: Function,
    default: sortItems
  },
  locale: {
    type: String,
    default: "en-US"
  },
  pagination: {
    type: Object,
    defualt: () => ({ ...defaultPagination })
  }
}, "data-iterator");
function toggle(key, oldBy, oldDesc, page, mustSort, multiSort) {
  let by = oldBy.slice();
  let desc = oldDesc.slice();
  const byIndex = by.findIndex((k) => k === key);
  if (byIndex < 0) {
    if (!multiSort) {
      by = [];
      desc = [];
    }
    by.push(key);
    desc.push(false);
  } else if (byIndex >= 0 && !desc[byIndex]) {
    desc[byIndex] = true;
  } else if (!mustSort) {
    by.splice(byIndex, 1);
    desc.splice(byIndex, 1);
  } else {
    desc[byIndex] = false;
  }
  if (!deepEqual(by, oldBy) || !deepEqual(desc, oldDesc)) {
    page = 1;
  }
  return { by, desc, page };
}
function useDataIterator(props) {
  const vm = getCurrentInstance("useDataIterator");
  const pagination = useProxiedModel(
    props,
    "pagination",
    props.pagination,
    (val) => {
      const pagination2 = { ...defaultPagination, ...val };
      return reactive({
        ...pagination2,
        page: Number(pagination2.page),
        perPage: Number(pagination2.perPage),
        total: props.remote ? Number(pagination2.total ?? props.items.length) : props.items.length
      });
    }
  );
  const sortBy = useProxiedModel(props, "sortBy");
  const sortDesc = useProxiedModel(props, "sortDesc");
  function updateOptions() {
    vm.emit("update:options", {
      pagination: pagination.value,
      sortBy: sortBy.value,
      sortDesc: sortDesc.value
    });
  }
  function sortItems2(items2) {
    return props.customSort(
      items2,
      wrapInArray(sortBy.value),
      wrapInArray(sortDesc.value),
      props.locale
    );
  }
  function paginateItems(items2) {
    return items2.slice(
      (pagination.value.page - 1) * pagination.value.perPage,
      Math.min(pagination.value.total, pagination.value.page * pagination.value.perPage)
    );
  }
  const items = computed(() => {
    if (props.remote)
      return props.items;
    let items2 = props.items.slice();
    items2 = sortItems2(items2);
    items2 = paginateItems(items2);
    return items2;
  });
  function sort(key) {
    if (Array.isArray(key)) {
      const options = {
        sortBy: key,
        sortDesc: key.map((s) => {
          const i = wrapInArray(sortBy.value).findIndex((k) => k === s);
          return i > -1 ? wrapInArray(sortDesc.value)[i] : false;
        }),
        pagination: {
          ...pagination.value
        }
      };
      sortBy.value = options.sortBy;
      sortDesc.value = options.sortDesc;
      vm.emit("update:options", options);
    } else {
      const { by, desc, page } = toggle(
        key,
        wrapInArray(sortBy.value),
        wrapInArray(sortDesc.value),
        pagination.value.page,
        props.mustSort,
        props.multiSort
      );
      const options = {
        sortBy: props.multiSort ? by : by[0] ?? null,
        sortDesc: props.multiSort ? desc : desc[0] ?? null,
        pagination: {
          ...pagination.value,
          page
        }
      };
      sortBy.value = options.sortBy;
      sortDesc.value = options.sortDesc;
      pagination.value.page = options.pagination.page;
      vm.emit("update:options", options);
    }
  }
  return {
    items,
    pagination,
    sortBy,
    sortDesc,
    sort,
    updateOptions
  };
}
export {
  makeDataIteratorProps,
  useDataIterator
};
//# sourceMappingURL=index.mjs.map
