import hashlib
import re


def _build_schedule_name(
    job_body_data: bytes,
    schedule: str,
    pipeline_name: str,
    display_name: str,
) -> str:
  """Generates the name for the schedule.

  Args:
    job_body_data: The serialized pipeline job.
    schedule: Schedule in cron format.
    pipeline_name: Full resource name of the pipeline in
      projects/<project>/pipelineJobs/<pipeline_id> format.
    display_name: Pipeline display name.
  Returns:
    Suggested schedule resource name.
  """
  pipeline_name_part = 'pipeline'
  if pipeline_name is not None:
    # pipeline_name format: projects/<project>/pipelineJobs/<pipeline_id>
    pipeline_id = pipeline_name.split('/')[-1]
    # Limiting the length of the pipeline name part.
    pipeline_name_part = pipeline_id[0:200]
  elif display_name is not None:
    pipeline_name_part = display_name
  pipeline_hash_part = hashlib.sha256(job_body_data).hexdigest()[0:8]
  schedule_part = (
      schedule.replace('*/', 'div').replace('*', 'a').replace(' ', '-'))
  job_name = '_'.join([
      'pipeline',
      pipeline_name_part,
      pipeline_hash_part,
      schedule_part,
  ])
  job_name = re.sub('[^-_a-z0-9]', '_', job_name)
  return job_name