public function createPosition(array $conditions, ?int $position = null, ?int $endPosition = null, bool $allowOutOfRange = false): int
    {
        $nextPosition = $this->getNextPosition($conditions);

        if ($position === null) {
            return $nextPosition;
        }

        if ($position < 0) {
            throw new BadStateException('position', 'Position cannot be negative.');
        }

        if (!$allowOutOfRange && $position > $nextPosition) {
            throw new BadStateException('position', 'Position is out of range.');
        }

        if ($endPosition !== null && $endPosition < $position) {
            throw new BadStateException('position', 'When creating a position, end position needs to be greater or equal than start position.');
        }

        $this->incrementPositions(
            $conditions,
            $position,
            $endPosition
        );

        return $position;
    }