const allowedMetadata = ['parentWebContentsId', 'teamId', 'loadSettings', 'perfTimer'];

/**
 * Assign preload metadata forwarded via `webPreferences.additionalArguments`
 * through `buildWebContentsPreloadMetadata` fn. This is common entrypoint of
 * our webapp / main browserWindow to share metadata context between processes.
 *
 * Note for some window (like child window from webapp, i.e calls) might not need
 * this metadata.
 */
const assignMetadata = () => {
  try {
    const metadataArgs = process.argv.filter((x) => x.includes('slack_preload_metadata_arguments')).shift() || `{}`;
    const metadataJson = JSON.parse(metadataArgs);
    Object.keys(metadataJson)
      .filter((key) => key !== 'identifier' && allowedMetadata.includes(key))
      .forEach((key) => window[key] = metadataJson[key]);
  } catch (e) {
    console.log(e); //tslint:disable-line:no-console
  }
};

module.exports = {
  assignMetadata
};
