@function __create-pad($string, $length, $chars: ' ') {
    $str-length: str-length($string);

    @if $str-length >= $length {
        @return '';
    }

    $pad-length: $length - $str-length;
    $chars: __to-string($chars);
    $chars-length: length($chars);
    $repeat-chars: if($chars-length > 0, ceil($pad-length / $chars-length), 0);

    @return str-slice(__repeat($chars, $repeat-chars), 1, $pad-length);
}


@function __pad($string, $length: 0, $chars: ' ') {
    $string: __base-to-string($string);
    $str-length: str-length($string);

    @if $str-length >= $length {
        @return $string;
    }

    $mid: ($length - $str-length) / 2;
    $left-length: floor($mid);
    $right-length: ceil($mid);
    $chars: __create-pad('', $right-length, $chars);

    @return str-slice($chars, 0, $left-length) + $string + $chars;
}


@function __pad-left($string, $length: 0, $chars: ' ') {
    $string: __base-to-string($string);

    @return if($string != '',
        __create-pad($string, $length, $chars) + $string,
        $string);
}


@function __pad-right($string, $length: 0, $chars: ' ') {
    $string: __base-to-string($string);

    @return if($string != '',
        $string + __create-pad($string, $length, $chars),
        $string);
}


/// Pads `$string` on the left and right sides if it is shorter then the given
/// padding length. The `$chars` string may be truncated if the number of padding
/// characters can't be evenly divided by the padding length.
///
///
/// @access public
/// @group String
/// @param {string} $string [''] - The string to pad.
/// @param {number} $length [0] - The padding length.
/// @param {string} $chars [' '] - The string used as padding.
/// @returns {string} Returns the padded string.
/// @example scss
/// $foo: _pad('abc', 8);
/// // => '  abc   '
/// $foo: _pad('abc', 8, '_-');
/// // => '_-abc_-_'
/// $foo: _pad('abc', 3);
/// // => 'abc'

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


/// Pads `$string` on the left side if it is shorter then the given padding
/// length. The `$chars` string may be truncated if the number of padding
/// characters exceeds the padding length.
///
///
/// @access public
/// @group String
/// @param {string} $string [''] - The string to pad.
/// @param {number} $length [0] - The padding length.
/// @param {string} $chars [' '] - The string used as padding.
/// @returns {string} Returns the padded string.
/// @example scss
/// $foo: _pad-left('abc', 6);
/// // => '   abc'
/// $foo: _pad-left('abc', 6, '_-');
/// // => '_-_abc'
/// $foo: _pad-left('abc', 3);
/// // => 'abc'

@function _pad-left($args...) {
    @return call(get-function('__pad-left'), $args...);
}


/// Pads `$string` on the right side if it is shorter then the given padding
/// length. The `$chars` string may be truncated if the number of padding
/// characters exceeds the padding length.
///
///
/// @access public
/// @group String
/// @param {string} $string [''] - The string to pad.
/// @param {number} $length [0] - The padding length.
/// @param {string} $chars [' '] - The string used as padding.
/// @returns {string} Returns the padded string.
/// @example scss
/// $foo: _pad-right('abc', 6);
/// // => 'abc   '
/// $foo: _pad-right('abc', 6, '_-');
/// // => 'abc_-_'
/// $foo: _pad-right('abc', 3);
/// // => 'abc'

@function _pad-right($args...) {
    @return call(get-function('__pad-right'), $args...);
}
