import { IGenericContext } from '../../../generic/types';
import { ISlackContext } from '../../types';
import { getMethods } from '../methods';

function omit(obj, key) {
  const { [key]: omitted, ...rest } = obj;
  return rest;
}

function transformContext ({ props, children }: IGenericContext): ISlackContext {
  const block_id = props?.slack?.block_id;
  
    const res: ISlackContext = {
      type: 'context',
      elements: children
        .filter(child => child.type === 'text' || child.type === 'image')  
        .map(child => omit(getMethods(child.type)(child), 'title'))
    };

    if (block_id) res.block_id = block_id;

    return res;
 }

export {
  transformContext
};