const resources = [
  { value: 'art', label: '作品' },
  { value: 'art-daily', label: '每日一图' },
  { value: 'art-album', label: 'Art Album' },
  { value: 'category', label: '分类管理' },
  { value: 'translate', label: '多语翻译' },
  { value: 'user', label: '用户' },
  { value: 'role', label: '角色' },
];

const actions = [
  { value: 'read', label: '查看' },
  { value: 'create', label: '创建' },
  { value: 'update', label: '更新' },
  { value: 'delete', label: '删除' },
];

const possessions = [
  { value: 'own', label: '自己的' },
  { value: 'any', label: '全部' },
];


const resourcesHash = resources.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});
const actionsHash = actions.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});
const possessionsHash = possessions.reduce((h, cur) => { h[cur.value] = cur.label; return h; }, {});


/**
 * 将grant信息翻译成人可读的
 * @param {*} grants 
 */
function translate(grant) {
  let { resource, action, possession, attributes } = grant;
  resource = resourcesHash[resource] || resource;
  action = actionsHash[action] || action;
  possession = possessionsHash[possession] || possession;
  return { resource, action, possession, attributes };
}


function fullGrants() {
  let grants = [];
  resources.forEach(res => {
    actions.forEach(act => {
      grants.push({
        resource: res.value,
        action: act.value,
        possession: 'any',
        attributes: '*',
      })
    });
  })
  return grants;
}



module.exports = { resources, actions, possessions, translate, fullGrants }
