function uniqueConfigID
%    UNIQUECONFIGID Determines a unique configuration ID for
%    each XCP UDP or CAN Configuration block copied over in a model.

% Copyright 2021 The MathWorks, Inc.

% Get the current block.
blk = gcb;
blkh = gcbh;
TLTypeStr = xcp.blkmasks.utils.getTLTypeStr(gcbh);

% @todo update the usage of edit-time filter filterOutInactiveVariantSubsystemChoices()
% instead use the post-compile filter activeVariants() - g2603543
allConfigBlocks = find_system(bdroot, ...
    'MatchFilter',@Simulink.match.internal.filterOutInactiveVariantSubsystemChoices,... % look only inside active choice of VSS
    'FollowLinks', 'on', ...
    'IncludeCommented',   'on', ...
    'LookUnderMasks', 'on', ...
    'MaskType', ['XCP ',TLTypeStr,' Configuration']);
currentBlockIndex = ismember(allConfigBlocks, blk);

% Remove the current block from the list.
allConfigBlocks(currentBlockIndex) = [];

% Loop through config blocks and find slave ID indices.
if isempty(allConfigBlocks) % Return, by default set to 1.
    set_param(blkh, 'SlaveID', '1');
    set_param(blkh, 'SlaveName', [TLTypeStr,'_Config1']);
    return;
end

% Continue if we have multiple slaves.
ids = zeros(1, length(allConfigBlocks));
for idx = 1: length(allConfigBlocks)
    ids(idx) = str2double(get_param(allConfigBlocks{idx}, 'SlaveID'));
end

ids = [0 unique(ids)];
diffs = diff(ids);
indices = find(diffs~=1);
if isempty(indices)
    uniqueSlaveID = length(ids);
else
    uniqueSlaveID = indices(1);
end

% Set it on the current block.
set_param(blkh, 'SlaveID', num2str(uniqueSlaveID));
set_param(blkh, 'SlaveName', strcat(TLTypeStr,'_Config', num2str(uniqueSlaveID)));

end