function this = hdlblkdlg(varargin)
%hdlblkdlg Construct a hdlblkdlg object.

%   Copyright 2005-2023 The MathWorks, Inc.
%     

this = slprops.hdlblkdlg;

if ~(builtin('license','test','Simulink_HDL_Coder'))
    warning(message('HDLShared:hdlshared:slhdlcoderinstall'));
    return;
end

if nargin < 2
    callFromHDLBlockPropsUI = false;
else
    callFromHDLBlockPropsUI = varargin{2};
end

if nargin >= 1
    arg1 = varargin{1};
    if ischar(arg1)
        handle = get_param(arg1, 'handle');
    else
        handle = arg1;
    end
    
    this.SLBlockHandle = handle;
end

% get a handle to the simulink block this object is sourced from
slbh = this.SLBlockHandle;

% get all the impl info from database,the default impl info from the
% configMgr, and the deprecated architecture name mapping if it exists.
[hdlblkAllImplInfo, defImpl, deprInfo, hdlblkInfoHide, helpInfo] = this.getSLBlkImplInfo(slbh);
% Check current hdl block params with params
sanitizeHDLDataParam(slbh);

% now update the block info
if ~isempty(defImpl) ...
        ...math and trig function blocks may not have any architectures for
        ...the selected function
        && ~isempty(hdlblkAllImplInfo)
    
    % stash away default implementation info
    this.defImplObj = defImpl;
    this.defArchSelection = defImpl.getArchitectureName;
    this.defArchInfo = defImpl.getImplParamInfo;
    this.helpInfo = helpInfo;
        
    % We will hide the HDL Block parameters that are marked to be hidden
    % only if this function is called from hdlContextMenu. This controls
    % the UI drawing. 
    if callFromHDLBlockPropsUI && ~isempty(hdlblkInfoHide)
        
        % iterate over all the registered architecture implementations
        archImplKeys = hdlblkAllImplInfo.keys;
        for archImplIndex = 1:length(archImplKeys)
            
            % get the current architecture implementation
            currArchImpl = archImplKeys{archImplIndex};
            
            % get the params that need to be hidden for the current
            % architecture implementation
            currentHideParam = hdlblkInfoHide(currArchImpl);

            if isempty(currentHideParam)
                continue;
            end

            currentKeys = hdlblkAllImplInfo(currArchImpl).keys;
            currentHideParam = intersect(currentKeys, currentHideParam);
            hdlblkAllImplInfo(currArchImpl).remove(currentHideParam);
        end
    end

    this.archInfo = hdlblkAllImplInfo;
    
    archName = defImpl.getArchitectureName;
    archInfoList = this.archInfo.keys;
    if isempty(archInfoList) || any(strcmp(archName, archInfoList))
        % set default if impl list is empty or contains the default option
        this.archSelection = archName;
    else
        % otherwise default to first arch selection
        this.archSelection = archInfoList{1};
    end
    
    % check if the simulink block has data associated and overlay it
    this.readInfoFromSLBlk(slbh, deprInfo);     
        
    registerListeners(this);
end


end
%--------------------------------------------------------------------------
function sanitizeHDLDataParam(slbh)
% Check current hdl block params with new params

hdlData = get_param(slbh, 'HDLData');
% % compare all p-v pairs except architecture
% if mismatch
%     set_param(slbh, 'HDLData',{});
% end


end

% LocalWords:  impl slbh
