goog.provide('grrUi.routing.aff4UrnToUrl');
goog.require('grrUi.core.fileDownloadUtils.vfsRoots');
goog.require('grrUi.core.utils.CLIENT_ID_RE');
goog.require('grrUi.core.utils.stripAff4Prefix');


/**
 * Returns a router configuration pointing to an AFF4 object with a given
 * URN or undefined if not corresponding href can be generated.
 *
 * @param {string} urn
 * @return {{state: string, params:Object}|null} Object with 2 keys: 'state'
 *     identifying router state and 'params' identifying router params.
 * @export
 */
grrUi.routing.aff4UrnToUrl = function(urn) {
  var components = grrUi.core.utils.stripAff4Prefix(urn).split('/');
  if (grrUi.core.utils.CLIENT_ID_RE.test(components[0])) {
    // Handle references to client object or to something within the
    // client namespace: flows or VFS files.

    if (grrUi.core.fileDownloadUtils.vfsRoots.includes(components[1])) {
      return {
        state: 'client.vfs',
        params: {
          clientId: components[0],
          path: components.slice(1).join('/')
        }
      };
    } else if (components[1] === 'flows' && components.length === 3) {
      return {
        state: 'client.flows',
        params: {
          clientId: components[0],
          flowId: components[2]
        }
      };
    } else {
      return {
        state: 'client',
        params: {
          clientId: components[0]
        }
      };
    }
  } else if (components[0] === 'hunts') {
    // Handle references to hunts.

    return {
      state: 'hunts',
      params: {
        huntId: components[1]
      }
    };
  } else if (components[0] === 'cron') {
    // Handle references to cron jobs.

    return {
      state: 'crons',
      params: {
        cronJobId: components[1]
      }
    };
  } else if (components[0] === 'ACL') {
    // Handle references to approvals.

    if (grrUi.core.utils.CLIENT_ID_RE.test(components[1])) {
      return {
        state: 'clientApproval',
        params: {
          clientId: components[1],
          username: components[components.length - 2],
          approvalId: components[components.length - 1]
        }
      };
    } else if (components[1] == 'hunts') {
      return {
        state: 'huntApproval',
        params: {
          huntId: components[2],
          username: components[components.length - 2],
          approvalId: components[components.length - 1]
        }
      };
    } else if (components[1] == 'cron') {
      return {
        state: 'cronJobApproval',
        params: {
          cronJobId: components[2],
          username: components[components.length - 2],
          approvalId: components[components.length - 1]
        }
      };
    }
  }

  return null;
};
