import { Component } from 'react';
import { connect } from 'react-redux';
import CurrentPrecondtions from './CurrentPrecondtions';
import PreconditionsList from './PreconditionsList';
import { CouponsPlus } from './globals';

export const getPreconditionsRow = rows => rows.find(row => row.type ===window.CouponsPlus.components.rows.types.preconditions)

class Preconditions extends Component {
    static mapStateToProps(state, props) {
        return {
            preconditionsRow: getPreconditionsRow(state.rows)
        }
    };

    getPreconditions() 
    {
        return this.getContext()?.conditionsOrFilters
    }

    getContext() 
    {
        return this.getColumn().contexts?.[0]
    }

    getColumn() 
    {
        return this.props.preconditionsRow.columns[0]
    }

    render() {
        return <div className="min-w-64 flex flex-col justify-between rounded-r-3 bg-gray-100 bg-opacity-50 py-3 px-3 space-y-2 overflow-y-scroll">
            <CurrentPrecondtions row={this.props.preconditionsRow}
                                 column={this.getColumn()}
                                 context={this.getContext()}
                                 preconditions={this.getPreconditions()}
            />
            <PreconditionsList row={this.props.preconditionsRow}
                               column={this.getColumn()}
                               context={this.getContext()}
                               preconditions={this.getPreconditions()}
            />
        </div>;
    }
}

export default connect(Preconditions.mapStateToProps, {})(Preconditions);
