@use 'sass:string';

// Convert kebab-case to camelCase
// Example: convert-kebab-case-to-camel-case('top-start') will return 'topStart'
@function convert-kebab-case-to-camel-case($string) {
    $camel-case: string.slice($string, 1); // Remove the first hyphen

    @for $i from 1 through string.length($camel-case) {
        $char: string.slice($camel-case, $i, $i);

        @if $char == '-' {
            $next-char: string.slice($camel-case, $i + 1, $i + 1);
            $camel-case: string.insert($camel-case, string.to-upper-case($next-char), $i + 2);
            $camel-case: string.slice($camel-case, 1, $i - 1) + string.slice($camel-case, $i + 2);
        }
    }

    @return $camel-case;
}

// Convert PascalCase to kebab-case
// Example: convert-pascal-case-to-kebab-case('TopStart') will return 'top-start'
@function convert-pascal-case-to-kebab-case($string) {
    $kebab-case: '';

    @for $i from 1 through string.length($string) {
        $char: string.slice($string, $i, $i);

        @if $i == 1 {
            $kebab-case: string.to-lower-case($char);
        } @else {
            @if string.to-upper-case($char) == $char {
                $kebab-case: $kebab-case + '-' + string.to-lower-case($char);
            } @else {
                $kebab-case: $kebab-case + $char;
            }
        }
    }

    @return $kebab-case;
}

// Replace all occurrences of a string with another string
// Example: replace('top-start', '-', '') will return 'topstart'
// Author: Kitty Giraudel
// See: https://kittygiraudel.com/2014/01/13/string-replacement-function-in-sass/
@function replace($string, $search, $replace: '') {
    $index: string.index($string, $search);

    @if $index {
        @return string.slice($string, 1, $index - 1) + $replace +
            replace(string.slice($string, $index + string.length($search)), $search, $replace);
    }

    @return $string;
}

// Singularize a string
// Example: singularize('gradients') will return 'gradient'
@function singularize($string) {
    @return string.slice($string, 1, string.length($string) - 1);
}

// Remove prefix from a string
// Example: remove-prefix('UNSTABLE_Component', 'UNSTABLE_') will return 'Component'
@function remove-prefix($string, $prefix) {
    @if string.index($string, $prefix) == 1 {
        @return string.slice($string, string.length($prefix) + 1);
    }

    @return $string;
}
