import copy


def _GenerateInferredAshFlags(device_config):
  """Generate runtime-packed ash flags into a single device config.

  Chrome flags are packed into /ui:serialized-ash-flags in the
  resultant runtime-only configuration, as a string of null-terminated
  strings.

  Args:
    device_config: transformed configuration for a single device.

  Returns:
    Config for a single device with /ui:serialized-ash-flags added.
  """
  ash_flags = set()
  ash_flags |= set(device_config.get('ui', {}).get('extra-ash-flags', []))

  help_content_id = device_config.get('ui', {}).get('help-content-id')
  if help_content_id:
    ash_flags.add('--device-help-content-id=%s' % help_content_id)

  if not ash_flags:
    return device_config

  serialized_ash_flags = ''
  for flag in sorted(ash_flags):
    serialized_ash_flags += '%s\0' % flag

  device_config = copy.deepcopy(device_config)
  device_config.setdefault('ui', {})
  device_config['ui']['serialized-ash-flags'] = serialized_ash_flags
  return device_config