import { TEMPLATE_ACCOUNT_ALL } from '@/modules/forms/constants';

import parseMetadataToState from '@/modules/forms/editor/helpers/metadata/v1/parseFormMetadataToState';

const toAutocompleteValue = (entity) =>
  entity
    ? {
        value: entity.id,
        label: entity.name,
      }
    : TEMPLATE_ACCOUNT_ALL;

const parseResponseState = (res, isDuplicate = false) => {
  const fromMetadata = parseMetadataToState(res.metadata, isDuplicate);

  const initialState = {
    id: res.id,
    category: res.formTemplateCategoryId,
    account: toAutocompleteValue(res.account),
    name: isDuplicate ? '' : res.name,
    title: res.title,
    image: res.logo,
    // data gets from metadata
    description: fromMetadata.description,
    dynamicFields: fromMetadata.dynamicFields,
    privacyPolicy: fromMetadata.privacyPolicy,
    termsConditions: fromMetadata.termsConditions,
    submitButton: fromMetadata.submitButton,
    skipButton: fromMetadata.skipButton,
    style: fromMetadata.style,
  };

  return initialState;
};

export default parseResponseState;
