@function __base-fill($list, $value, $start: null, $end: null) {
    $length: length($list);

    $start: if($start == null,
        1,
        if(__is-number($start) and $start != 0,
            $start,
            if(__is-truthy($start), 2, 1)));

    @if $start < 0 {
        $start: if(-$start >= $length, 1, $length + $start + 1);
    }

    $end: if(__is-undefined($end) or $end >= $length,
        $length,
        if(__is-number($end), $end, if($end, 2, 1)));

    @if ($end <= 0) {
        $end: $end + $length;
    }

    $length: if($start > $end, 0, $end);

    @while $start <= $length {
        $list: set-nth($list, $start, $value);

        $start: $start + 1;
    }

    @return $list;
}


@function __fill($list, $value: null, $start: 1, $end: length($list)) {
    $length: if($list, length($list), 0);

    @if not ($length) {
        @return ();
    }

    @if $start
    and __is-number($start)
    and __is-iteratee-call($list, $value, $start) {
        $start: 1;
        $end: $length;
    }

    @return __base-fill($list, $value, $start, $end);
}


/// Fills elements of `$list` with `$value` from `$start` up to, but not
/// including, `$end`. Returns new list.
///
///
/// @access public
/// @group List
/// @param {List} $list - The list to fill.
/// @param {*} $value [null] - The value to fill `$list` with.
/// @param {number} $start [1] - The start position.
/// @param {number} $end [length($list)] - The end position.
/// @returns {List} Returns new list with filled values.
/// @example scss
/// $list: 1 2 3;
/// 
/// $foo: _fill($list, 'a');
/// // => 'a' 'a' 'a'
/// 
/// $foo: _fill($list, 'a', 2);
/// // => 1 'a' 'a'

@function _fill($args...) {
    @return call(get-function('__fill'), $args...);
}
