function getRuntimeContext(options = {}) {
  return (options || {}).runtimeContext || null;
}

function getRuntimeEnvironment(options = {}) {
  const runtimeContext = getRuntimeContext(options);

  if (!runtimeContext) return null;
  if (runtimeContext.mode === 'stage') return 'stage';
  if (runtimeContext.mode === 'production') return 'production';

  return null;
}

function getRuntimeProjectSlug(options = {}) {
  const runtimeContext = getRuntimeContext(options);
  return runtimeContext?.projectSlug || null;
}

function applyRuntimeEnvironment(where = {}, options = {}) {
  const environment = getRuntimeEnvironment(options);

  if (!environment) return where;

  return {
    ...where,
    environment,
  };
}

function applyRuntimeProjectFilter(projectInclude = {}, options = {}) {
  const projectSlug = getRuntimeProjectSlug(options);

  if (!projectSlug) return projectInclude;

  return {
    ...projectInclude,
    required: true,
    where: {
      ...(projectInclude.where || {}),
      slug: projectSlug,
    },
  };
}

module.exports = {
  applyRuntimeEnvironment,
  applyRuntimeProjectFilter,
  getRuntimeEnvironment,
  getRuntimeProjectSlug,
};
