function outTimeseriesNotCompact = createDefaultTimeSeries(portInfo,portCompiledInfo,simStartTime,simStopTime,modelH)
%

%   Copyright 2020-2023 The MathWorks, Inc.

outTimeseriesNotCompact = cell(length(portInfo),1);
% if the model contains no bus element ports return the default value

% outTimeseries would be an array where the output ports are indexed by
% their port numbers
assert(length(portInfo) == length(portCompiledInfo));
for idx = 1:length(portInfo)
    out_idx_info = portInfo{idx};
    if iscell(out_idx_info)
        out_idx_info = out_idx_info{1};
    end
    blockH = get_param(out_idx_info(1).BlockPath,'handle');
    isCurrentPortBEP = sldvshareprivate('isBusElem',blockH);
    if ~isCurrentPortBEP
        continue;
    end
    portAttributes = portCompiledInfo(idx);
    busSampleTime = portAttributes.SampleTime(1) - portAttributes.SampleTime(2);
    dimensions = portAttributes.Dimensions;
    time = 0;
    for jdx = 1:length(portAttributes.compiledInfo)
        % sample time for the default signal
        sampleTime = portAttributes.compiledInfo(jdx).SampleTime;
        if(sampleTime == -1)
            sampleTime = busSampleTime;
        end
        
        % dataType for the default signal
        dataType = portAttributes.compiledInfo(jdx).DataType;
        if ~(portAttributes.compiledInfo(jdx).Dimensions == -1)
            dimensions = portAttributes.compiledInfo(jdx).Dimensions;
        end
        yout = getDefaultValueOfDataType(dataType,modelH);
        try
            time = simStartTime:sampleTime(1):simStopTime;
            timeSteps = length(time);
            if(length(dimensions) == 1)
                % If the signal dimension is 1, the signal should be
                % repeated timestep times
                yout = repmat(yout, [timeSteps dimensions]);
            else
                yout = repmat(yout,[dimensions timeSteps]);
            end
            defaultValue = timeseries(feval(dataType,yout),time); %#ok<*NASGU> % used as a string
        catch
            defaultValue = timeseries(yout,time);
        end
        eval([portAttributes.compiledInfo(jdx).SignalPath '=defaultValue;']);
    end
    portName = get_param(blockH,'portName');
    eval(['outTimeseriesNotCompact{idx}  = ' portName ';']);
end
end

function defaultValue = getDefaultValueOfDataType(signalDataTypeStr,modelH)
% TO DO: refactor this code as a part of g2164220
    untypedData = 0;
    if sldvshareprivate('util_is_simulink_builtin', signalDataTypeStr)
        if strcmp(signalDataTypeStr, 'boolean') || strcmp(signalDataTypeStr, 'bool')
            defaultValue = cast(untypedData,'logical');
        else
            defaultValue = cast(untypedData,signalDataTypeStr);
        end
    else
        % get default value for enum, not necessarily 0
        [isEnum, className] = sldvshareprivate('util_is_enum_type', signalDataTypeStr);
        if isEnum
            defaultValue = sldvshareprivate('util_get_enum_defaultvalue', className);
        else
            [isfxptype, fxpTypeInfo] = sldvshareprivate('util_is_fxp_type', signalDataTypeStr, modelH);
            if isfxptype && ~isempty(fxpTypeInfo)            
                defaultValue = fi(untypedData, fxpTypeInfo);
            else
                 error(message('Sldv:MdlPrepareTestdataForInport:UnrecognizedTypeForTestCase', signalDataTypeStr));  
            end
        end
    end
end

% LocalWords:  timestep