// DOC: https://api.slack.com/reference/block-kit/blocks#section
import { IGenericSection } from '../../../generic/types';
import { ISlackSection } from '../../types';

import { transformText } from './text-object';
import { getMethods } from '../methods';

export const transformSection = ({
  text,
  block_id,
  fields,
  accessory
}: ISlackSection): IGenericSection => {
  const res: IGenericSection = {
    type: 'section',
    children: [],
    props: {
      slack: {
        type: 'section'
      }
    }
  };
  
  if (text) {
    res.children.push(transformText(text));
  }

  if (accessory) {
    res.children.push(getMethods(accessory.type)(accessory));
  }

  if (fields) {
    const fieldSection = {
      type: 'section',
      props: { 
        column: 2,
        slack: {
          type: 'fields'
        }
      },
      children: []
    };
    for (let i = 0; i < fields.length; i++) {
      fieldSection.children.push(transformText(fields[i]));
    }
    res.children.push(fieldSection);
  }

  if (block_id) {
    res.props.slack.block_id = block_id;
  }

  return res;
};