<commit_before>import getOppositePlacement from '../utils/getOppositePlacement';
import getPopperClientRect from '../utils/getPopperClientRect';
import getOffsets from '../utils/getOffsets';
import isModifierRequired from '../utils/isModifierRequired';
import runModifiers from '../utils/runModifiers';

/**
 * Modifier used to flip the placement of the popper when the latter is starting overlapping its reference element.
 * Requires the `preventOverflow` modifier before it in order to work.
 * **NOTE:** data.instance modifier will run all its previous modifiers everytime it tries to flip the popper!
 * @method
 * @memberof Modifiers
 * @argument {Object} data - The data object generated by update method
 * @argument {Object} options - Modifiers configuration and options
 * @returns {Object} The data object, properly modified
 */
export default function flip(data, options) {
    // check if preventOverflow is in the list of modifiers before the flip modifier.
    // otherwise flip would not work as expected.
    if (!isModifierRequired(data.instance.modifiers, 'flip', 'preventOverflow')) {
        console.warn('WARNING: preventOverflow modifier is required by flip modifier in order to work, be sure to include it before flip!');
        return data;
    }

    if (data.flipped && data.placement === data.originalPlacement) {
        // seems like flip is trying to loop, probably there's not enough space on any of the flippable sides
        return data;
    }

    let placement = data.placement.split('-')[0];
    let placementOpposite = getOppositePlacement(placement);
    const constiation = data.placement.split('-')[1] || '';

    let flipOrder = [];
    if(options.behavior === 'flip') {
        flipOrder = [
            placement,
            placementOpposite
        ];
    } else {
        flipOrder = options.behavior;
    }

    flipOrder.forEach((step, index) => {
        if (placement !== step || flipOrder.length === index + 1) {
            return data;
        }

        placement = data.placement.split('-')[0];
        placementOpposite = getOppositePlacement(placement);

        const popperOffsets = getPopperClientRect(data.offsets.popper);

        // this boolean is used to distinguish right and bottom from top and left
        // they need different computations to get flipped
        const a = ['right', 'bottom'].indexOf(placement) !== -1;

        // using Math.floor because the reference offsets may contain decimals we are not going to consider here
        if (
            a && Math.floor(data.offsets.reference[placement]) > Math.floor(popperOffsets[placementOpposite]) ||
            !a && Math.floor(data.offsets.reference[placement]) < Math.floor(popperOffsets[placementOpposite])
        ) {
            // this boolean to detect any flip loop
            data.flipped = true;
            data.placement = flipOrder[index + 1];
            if (constiation) {
                data.placement += '-' + constiation;
            }
            data.offsets.popper = getOffsets(data.instance.state, data.instance.popper, data.instance.reference, data.placement).popper;

            data = runModifiers(data.instance.modifiers, data.instance.options, data, 'flip');
        }
    });
    return data;
}<commit_msg>fixed typo<commit_after>import getOppositePlacement from '../utils/getOppositePlacement';
import getPopperClientRect from '../utils/getPopperClientRect';
import getOffsets from '../utils/getOffsets';
import isModifierRequired from '../utils/isModifierRequired';
import runModifiers from '../utils/runModifiers';

/**
 * Modifier used to flip the placement of the popper when the latter is starting overlapping its reference element.
 * Requires the `preventOverflow` modifier before it in order to work.
 * **NOTE:** data.instance modifier will run all its previous modifiers everytime it tries to flip the popper!
 * @method
 * @memberof Modifiers
 * @argument {Object} data - The data object generated by update method
 * @argument {Object} options - Modifiers configuration and options
 * @returns {Object} The data object, properly modified
 */
export default function flip(data, options) {
    // check if preventOverflow is in the list of modifiers before the flip modifier.
    // otherwise flip would not work as expected.
    if (!isModifierRequired(data.instance.modifiers, 'flip', 'preventOverflow')) {
        console.warn('WARNING: preventOverflow modifier is required by flip modifier in order to work, be sure to include it before flip!');
        return data;
    }

    if (data.flipped && data.placement === data.originalPlacement) {
        // seems like flip is trying to loop, probably there's not enough space on any of the flippable sides
        return data;
    }

    let placement = data.placement.split('-')[0];
    let placementOpposite = getOppositePlacement(placement);
    const variation = data.placement.split('-')[1] || '';

    let flipOrder = [];
    if(options.behavior === 'flip') {
        flipOrder = [
            placement,
            placementOpposite
        ];
    } else {
        flipOrder = options.behavior;
    }

    flipOrder.forEach((step, index) => {
        if (placement !== step || flipOrder.length === index + 1) {
            return data;
        }

        placement = data.placement.split('-')[0];
        placementOpposite = getOppositePlacement(placement);

        const popperOffsets = getPopperClientRect(data.offsets.popper);

        // this boolean is used to distinguish right and bottom from top and left
        // they need different computations to get flipped
        const a = ['right', 'bottom'].indexOf(placement) !== -1;

        // using Math.floor because the reference offsets may contain decimals we are not going to consider here
        if (
            a && Math.floor(data.offsets.reference[placement]) > Math.floor(popperOffsets[placementOpposite]) ||
            !a && Math.floor(data.offsets.reference[placement]) < Math.floor(popperOffsets[placementOpposite])
        ) {
            // this boolean to detect any flip loop
            data.flipped = true;
            data.placement = flipOrder[index + 1];
            if (variation) {
                data.placement += '-' + variation;
            }
            data.offsets.popper = getOffsets(data.instance.state, data.instance.popper, data.instance.reference, data.placement).popper;

            data = runModifiers(data.instance.modifiers, data.instance.options, data, 'flip');
        }
    });
    return data;
}
{'fraction_non_alphanumeric': 0.10524705522410892, 'fraction_numerical': 0.0018357044515832951, 'mean_word_length': 2.887039239001189, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621514', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1663, 'n_words': 555}