var helpers = require('./helpers');
var actionId = helpers.actionId;
var useKey = helpers.useKey;

var ATTRIBUTES = {
  name: actionId('set_{0}_name'),
  short_name: actionId('set_{0}_short_name'),
  not_found_page_cache_globally: actionId('set_{0}_not_found_page_cache_globally'),
  attributes: actionId('set_{0}_attributes')
};

var ACTIONS = {
  add_web_path: actionId('add_{0}_path'),
  create_asset: actionId('create_{0}'),
  create_link: actionId('link_notice_{0}_to_{1}', 2),
  set_attribute_value: ATTRIBUTES
};

var LINKS = {
  TYPE_1: 1,
  TYPE_2: 2,
  TYPE_3: 4,
  NOTICE: 8
};

function getActionId(type) {
  var args = Array.prototype.slice.call(arguments, 1);
  var action = ACTIONS[useKey(type)];
  if (typeof action === 'object')
    action = action[useKey(args.shift())];
  return action.apply(null, args);
}

function actionAddPath(opts) {
  var type = 'add_web_path';

  return {
    action_id: getActionId.apply(null, [type, opts.id]),
    action_type: type,
    asset: opts.assetId, // id of asset performing action against
    path: opts.path // web path
  };
}

function actionCreateAsset(opts) {
  var type = 'create_asset';

  return {
    action_id: getActionId.apply(null, [type, opts.id]),
    action_type: type,
    type_code: opts.type,
    parentid: opts.parentId || 1,
    value: opts.value || '',
    link_type: opts.link || LINKS['TYPE_1'],
    is_dependant: opts.dependant || 0,
    is_exclusive: opts.exclusive || 0
  };
}

function actionCreateLink(opts) {
  var type = 'create_link';

  return {
    action_id: getActionId.apply(null, [type, opts.to, opts.from]),
    action_type: type,
    asset: opts.assetId,
    value: opts.value || '',
    link_type: opts.link || LINKS['TYPE_1'],
    is_dependant: opts.dependant || 0,
    is_exclusive: opts.exclusive || 0,
    assetid: '', // id of asset linking to
    is_major: '' // ???
  };
}

function actionSetAttribute(opts) {
  var type = 'set_attribute_value';

  return {
    action_id: getActionId.apply(null, [type, opts.attribute, opts.id]),
    action_type: type,
    asset: opts.assetId,
    attribute: opts.attribute,
    value: opts.value || ''
  };
}

exports.createAction = function createAction(type, opts) {
  type = useKey(type);

  if (type === 'add_web_path') {
    return actionAddPath(opts);
  }

  if (type === 'create_asset') {
    return actionCreateAsset(opts);
  }

  if (type === 'set_attribute_value') {
    return actionSetAttribute(opts);
  }

  if (type === 'create_link') {
    return actionCreateLink(opts);
  }

  throw new Error('Unknown action type of \'' + type + '\'');
};
exports.getActionId = getActionId;

@@ -509,16 +509,77 @@
 TRIBUTES
+,%0A  set_permission: actionId('set_permission_%7B0%7D_%7B1%7D_%7B2%7D', 3)
 %0A%7D;%0A%0Avar
@@ -2203,16 +2203,343 @@
   %7D;%0A%7D%0A%0A
+function actionSetPermission(opts) %7B%0A  var type = 'set_permission';%0A%0A  return %7B%0A    action_id: getActionId.apply(null, %5Btype, opts.assetId, opts.permission, opts.userId%5D),%0A    action_type: type,%0A    asset: opts.assetId,%0A    permission: opts.permission %7C%7C 1,%0A    granted: opts.granted %7C%7C 1,%0A    userid: opts.userId %7C%7C 7%0A  %7D;%0A%7D%0A%0A
 exports.
@@ -2749,32 +2749,104 @@
 set(opts);%0A  %7D%0A%0A
+  if (type === 'create_link') %7B%0A    return actionCreateLink(opts);%0A  %7D%0A%0A
   if (type === '
@@ -2923,35 +2923,38 @@
  (type === '
-create_link
+set_permission
 ') %7B%0A    ret
@@ -2959,34 +2959,37 @@
 eturn action
-CreateLink
+SetPermission
 (opts);%0A  %7D%0A

Update set permission action