import { computed, onMounted, ref, unref } from "vue";
import { ContentConfig } from "/@/components/PageContent";
import { useUserStoreHook } from "/@/store/modules/user";

type Params = {
  pageName: string;
  callback?: (permissions: Array<string>) => void;
  contentConfig?: ContentConfig;
};

export function usePermission({ pageName, callback, contentConfig }: Params) {
  const permissions = ref<Array<string>>([]);
  const contentConfigRef = computed(() => contentConfig);

  const permission = computed(() => {
    return {
      list: permissions.value.some(i => i === "001"),
      create: permissions.value.some(i => i === "002"),
      detail: permissions.value.some(i => i === "007")
    };
  });

  onMounted(() => {
    console.log(useUserStoreHook().menuActions);

    permissions.value = useUserStoreHook().getMenuActions(pageName);
    callback && callback(unref(permissions));
  });

  return {
    contentConfigRef,
    permissions,
    permission
  };
}
