export function fromQuestionnaireData(data: Record<string, any>): {
  storage: QuestionnaireStorage;
  model: QuestionnaireModel;
} {
  const { healthAnalysisReportId: reportId, dialogId } = data;
  const nextQuestions = data.nextQuestions ?? [];
  return {
    storage: reportId ? { questions: [] } : { dialogId, questions: nextQuestions },
    model: { reportId, questionnaires: fromQuestionnaireProps(nextQuestions) },
  };
}

export function toQuestionnaireData(data: QuestionnaireProps[], { dialogId, questions }: QuestionnaireStorage) {
  for ( const item of data ) {
    switch ( item.name ) {
      case 'select':
        const update = (options: SelectQuestionnaireProps['options'], _options?: Record<string, any>[]) => {
          if ( !options?.length || !_options?.length ) return;

          for ( const option of options ) {
            const _option = _options?.find(_ => _.id === option.id);
            if ( !_option ) continue;

            _option.checked = option.checked;

            update(option.options, _option?.options);
          }
        };


        for ( const item of data ) {
          const _item = questions.find(_ => _.id === item.id);
          update(item.options, _item?.options);
        }
        break;
    }
  }
  return { dialogId, questions };
}

function fromQuestionnaireProps(data: Record<string, any>[]): QuestionnaireProps[] {
  const questionnaires: QuestionnaireProps[] = [];
  for ( const item of data ) {
    const { css, classify } = item;
    if ( css === 'hide' || classify?.endsWith('_result') ) continue;

    switch ( css ) {
      case 'select':
      case 'checkbox':
        const { id, title: label, required = false } = item;
        const multiple = css === 'checkbox';

        const _options = (data: Record<string, any>[]) => {
          if ( !Array.isArray(data) ) return [];
          const options: SelectQuestionnaireProps['options'] = [];
          for ( const option of data ) {
            const { css } = option;
            if ( css === 'hide' ) continue;

            const { id, name: label } = option;
            const multiple = css === 'checkbox';
            const children = _options(option.options);
            const checked = option.checked || children.some(option => option.checked);
            options.push({
              id, label, checked,
              multiple, options: children,

              value: children.filter(option => option.checked).map(option => option.label).join(' '),
            });
          }
          return options;
        };

        questionnaires.push(<SelectQuestionnaireProps> {
          name: 'select', id, label, required,
          multiple, options: _options(item.options),
        });
        break;
    }
  }
  return questionnaires;
}


export interface QuestionnaireStorage {
  dialogId?: string;
  questions: Record<string, any>[];
}

export interface QuestionnaireModel {
  reportId?: string;
  questionnaires: QuestionnaireProps[];
}

export type QuestionnaireProps =
  | SelectQuestionnaireProps

export interface SelectQuestionnaireProps {
  name: 'select';
  id: string;
  label: string;
  required?: boolean;
  options: {
    id: string;
    label: string;
    checked?: boolean;

    multiple?: boolean;
    options: SelectQuestionnaireProps['options'];

    value?: string;
  }[];
  multiple?: boolean;
}
