function indentedText = indentcode(text,language)
    % INDENTCODE Indents code.
    %   I = INDENTCODE(T) indents the text T according to the MATLAB
    %   language indenting preferences. When indenting text, INDENTCODE
    %   retains the same line separator style used by the input text.
    %
    %   I = INDENTCODE(T,L) indents the text according to the indenting
    %   preferences for the specified language L. The specified language L
    %   must be one of the following:
    %       'c', 'java', 'matlab', 'simscape', 'xml'.
    %
    %   This file is for internal use only and is subject to change without
    %   notice.

    %   Copyright 2012-2023 The MathWorks, Inc.

    narginchk(1,2)

    if nargin < 2
        language = 'matlab';
    end

    if ~ischar(text) && ~isStringScalar(text)
        error(message('MATLAB:INDENTCODE:NotString'))
    end

    if ~ischar(language) && ~isStringScalar(language)
        error(message('MATLAB:INDENTCODE:InvalidLanguage'))
    end

    % Convert any string inputs to character vectors
    text = convertStringsToChars(text);
    language = convertStringsToChars(language);

    % Return early if empty input
    if strcmp(text, '')
        indentedText = text;
        return;
    end

    % Perform indenting
    if strcmp(language, 'simscape')
        if isdeployed
            % No-op for Simscape, as that is no longer supported in Java
            indentedText = text;
        else
            indentCodeManager = matlab.internal.IndentCodeManager(language);
            cleanup = onCleanup(@()indentCodeManager.close());
            indentedText = indentCodeManager.indentcode(text);
        end
    else
        indentedText = matlab.internal.indentcodeLegacy(text, language);
    end
end