'use strict';

import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup';
import {Button} from './Button';

/** @alias GridRowActions~AlignTypes */
const AlignClassNameMaps = {
    left: 'pull-left',
    right: 'pull-right',
    none: 'grid-row-inline-action-group'
};

/** @name GridRowActions~AlignTypes */
export const AlignTypes = _.mapValues(AlignClassNameMaps, (val, key) => key);

export class GridRowActions extends React.Component {
    static propTypes = {
        actions: PropTypes.array,
        align: PropTypes.oneOf(_.keys(AlignClassNameMaps)),
        children: PropTypes.node
    };

    static defaultProps = {
        actions: [],
        align: AlignTypes.right,
        children: null
    };

    static AlignTypes = AlignTypes;

    getAlignClassName() {
        return AlignClassNameMaps.hasOwnProperty(this.props.align)
            ? AlignClassNameMaps[this.props.align]
            : AlignClassNameMaps.right;
    }

    render() {
        let legacyActions = this.props.actions.map((action, i) => {
            return (
                <Button
                    bsStyle="link"
                    data-test-automation-id={action.testAutomationId}
                    onClick={action.onClick}
                    key={i}>
                    {action.text}
                </Button>
            );
        });

        return (
            <ButtonGroup className={`${this.getAlignClassName()} rc-gridRowActions`}>
                {legacyActions.concat(this.props.children || [])}
            </ButtonGroup>
        );
    }
}
