export const parseFieldList = (value = '') =>
  String(value)
    .split(',')
    .map((item) => item.trim())
    .filter(Boolean);

export const buildPublicFormUrl = (origin, slug) =>
  `${String(origin || '').replace(/\/$/, '')}/forms/public/${slug}`;

export const buildFormBuilderPublicEndpoint = (backendOrigin, slug) =>
  `${String(backendOrigin || '').replace(/\/$/, '')}/public/builder-forms/${slug}/submit`;

export const buildSubmissionPayload = (fields = [], values = {}) =>
  fields.reduce((acc, field) => {
    const key = typeof field === 'string' ? field : field?.key;
    const value = values[key];
    if (key && value !== undefined && value !== null && value !== '') {
      acc[key] = value;
    }
    return acc;
  }, {});
