function obj = repmat(obj,varargin)
%REPMAT Replicate and tile an array.
%   B = REPMAT(A, M, N)
%   B = REPMAT(A, [M,N])
%   B = REPMAT(A, N)
%   B = REPMAT(A, P1, P2, ..., Pn)
%   B = REPMAT(A, [P1, P2, ..., Pn])
%
%   If ndims(A) == ndims(B) then B has the same dimension labels as A.
%   Otherwise, the extra dimensions in B get the 'U' dimension label.
%   
%   See also REPMAT, DLARRAY.

%   Copyright 2023 The MathWorks, Inc.

isObjDlarray = isa(obj,'dlarray');
if ~isObjDlarray
    % There must be a dlarray elsewhere in the input list.  Extract all the
    % inputs as user-facing numeric array types and re-dispatch the call.
    varargin = extractDlarrayCell(varargin);
    obj = repmat(obj, varargin{:});
    return
end

% Check for any dlarrays in the input list and extract permuted values, but
% allow internal data types so that we do not lose tracing.
for ii=1:numel(varargin)
    arg = varargin{ii};
    if isa(arg, 'dlarray')
        arg = permuteExplicitly(arg);
        varargin{ii} = arg.FormattedData.Data;
    end
end

obj = permuteExplicitly(obj);
[fdobj, objdata] = extractData(obj.FormattedData);

objdata = internal_repmat(objdata, varargin{:});

% If the output has more dimensions than input, add extra 'U' dimensions to
% the output. Since we permuted explicitly beforehand, viewToData in the
% call below is empty.
dimsobj = dims(obj);
viewToData = [];
[dimsobj, objdata, viewToData] = addOrDropDimensionLabels(dimsobj, objdata, viewToData);

fdobj.DimensionMetadata = dimsobj;
obj.FormattedData = insertData(fdobj, objdata);
obj.ViewToDataMap = viewToData;
end
