%

% Copyright 2017-2020 The MathWorks, Inc.
function [vTs, elementName] = parseElement(element, elementName, parsedElem)
    vTs = element;
    if isa(element, 'Simulink.SimulationData.Element')
        elementName = getElementName(element, elementName, parsedElem);
        arrayOfBusErrorCheck(element, parsedElem);
        vTs = element.Values;
    elseif isstruct(element)
        arrayOfBusErrorCheck(element, parsedElem);
    end
end

function arrayOfBusErrorCheck(element, parsedElem)
    values = element;
    if ~isstruct(element)
        values = element.Values;
    end

    if isstruct(values) && ~isscalar(values)
        error(message('sl_xls:WriteTable:ArrayOfBusesError', getAoBName(parsedElem)));
    end
end

function name = getAoBName(parsedElem)
    name = parsedElem.getBlockSource;
    if strlength(name) == 0
        name = parsedElem.VariableName;
    end
end

function elementName = getElementName(element, elementName, parsedElem)
    if strlength(elementName) == 0
        % use propagated name if element name is empty
        elementName = element.PropagatedName;
    end

    if strlength(elementName) == 0
        % use signal label if element is still empty
        elementName = parsedElem.getChildren{1}.getSignalLabel;
    end

    elementName = string(elementName);
end
