function dlnet = dag2dlnetwork(dagnet)
% 

%   Copyright 2023 The MathWorks, Inc.

arguments
    dagnet (1,1) {iValidateIsADagOrSeriesNetwork(dagnet)}
end

[dlnet, info] = deep.internal.sdk.dag2dlnetwork(dagnet, ...
    RemoveFoldingLayers=false, ...
    RemoveSingletonSpatialOutputs=true, ...
    ErrorForInvalidNetworks=false);

if ~info.ConversionSuccessful
    if ~isempty(info.UnsupportedLayerNames)
        unsupportedLayerNamesStr = iConvertLayerNamesStrArrayToErrorStr(info.UnsupportedLayerNames);
        warning(message('nnet_cnn:dag2dlnetwork:UnsupportedLayers', unsupportedLayerNamesStr));
    else
        warning(message('nnet_cnn:dag2dlnetwork:NetworkCouldNotBeConverted'));
    end
end
end

function iValidateIsADagOrSeriesNetwork(net)
if ~(isa(net, 'DAGNetwork') || isa(net, 'SeriesNetwork'))
    error(message('nnet_cnn:dag2dlnetwork:NotADagOrSeriesNetwork'));
end
end

function errorStr = iConvertLayerNamesStrArrayToErrorStr(layerNames)
% Add double quotes to each layer name and join the layer array into a
% single str for the error.
layerNames = """" + layerNames + """";
errorStr = strjoin(layerNames, ', ');
end