import { mapGetters } from 'vuex';
import { Organisations } from '@dougal/organisations';

export default {
  name: "AccessMixin",

  computed: {
    ...mapGetters(['user', 'projectConfiguration'])
  },

  methods: {

    access (operation, organisations) {
      if (this.user) {
        if (!organisations) organisations = this.projectConfiguration?.organisations;
        if (!organisations instanceof Organisations) {
          organisations = new Organisations(organisations);
        }
        return this.user.canDo(operation, organisations);
      }
    },

    readaccess (item) {
      return this.access('read', item);
    },

    writeaccess (item) {
      return this.access('write', item);
    },

    adminaccess (item) {
      return this.access('edit', item);
    }
  }
}
