import * as selectors from '../redux/selectors';
import { createFormMetadata } from '@/modules/forms/editor/helpers/metadata/v1/createFormMetadata';

// todo: delete
export const checkIsValidToSaveCalculation = (state$) => {
  const name = selectors.nameSelector(state$);
  const title = selectors.titleSelector(state$);
  const dynamicFields = selectors.dynamicFieldsSelector(state$);
  const privacyPolicy = selectors.privacyPolicySelector(state$);
  const submitButton = selectors.submitButtonSelector(state$);
  const termsConditions = selectors.termsConditionsSelector(state$);
  const skipButton = selectors.skipButtonSelector(state$);

  const isValid = !!(
    name &&
    title &&
    dynamicFields.length &&
    dynamicFields.every((field) => field.value) &&
    privacyPolicy.title &&
    submitButton.title &&
    (termsConditions.isActive ? termsConditions.title : true) &&
    (skipButton.isActive ? skipButton.title : true)
  );

  return isValid;
};

export const getFormMetadataCalculation = (state$) => {
  const state = {
    name: selectors.nameSelector(state$),
    style: selectors.styleSelector(state$),
    privacyPolicy: selectors.privacyPolicySelector(state$),
    termsConditions: selectors.termsConditionsSelector(state$),
    skipButton: selectors.skipButtonSelector(state$),
    submitButton: selectors.submitButtonSelector(state$),
    image: selectors.imageSelector(state$),
    title: selectors.titleSelector(state$),
    description: selectors.descriptionSelector(state$),
    dynamicFields: selectors.dynamicFieldsSelector(state$),
  };
  return createFormMetadata(state);
};
