import { t } from '@grafana/i18n';

import { type RepositorySpec, type RepositoryViewList } from '../../../api/clients/provisioning/v0alpha1';
import { type RepositoryFormData } from '../types';
import { specToData } from '../utils/data';

export interface GetDefaultValuesOptions {
  repository?: RepositorySpec;
  allowedTargets?: RepositoryViewList['allowedTargets'];
}

export function getDefaultValues({
  repository,
  allowedTargets = ['folder'],
}: GetDefaultValuesOptions = {}): RepositoryFormData {
  if (!repository) {
    const defaultTarget = allowedTargets.includes('folder') ? 'folder' : 'instance';

    return {
      type: 'github',
      title: t('provisioning.get-default-values.title.repository', 'Repository'),
      token: '',
      url: '',
      branch: '',
      generateDashboardPreviews: false,
      readOnly: false,
      prWorkflow: true,
      path: '',
      sync: {
        enabled: false,
        target: defaultTarget,
        intervalSeconds: 60,
      },
      enablePushToConfiguredBranch: false,
    };
  }
  return specToData(repository);
}
