import { JsonObject } from '@win2win/shared';
import { useFetch } from '@win2win/shared-ui';
import { orderBy } from 'lodash';
import { Option } from 'src/models';
import { computed } from 'vue';

export function usePermissions() {
  const { data, fetching } = useFetch<JsonObject[]>(['permissions', { all: true }], '/permissions', {}, undefined, {
    staleTime: 1000 * 3600 * 24,
    defaultValue: [],
  });

  const permissions = computed(() =>
    orderBy(
      data.value.map((item) => ({ label: `${item.ID_PERMISSION} - ${item.DESCRIPCION_PER}`, value: item.ID_PERMISSION } as Option)),
      'value'
    )
  );
  return { data: permissions, fetching };
}
