{
 Author: support@idimager.com
 Version: 1.0.5
 Last Change: 2023-08-08; added InternalMetaCascadeForItem to allow more details to be cascaded
      Change: 2023-08-07; fix for changed parameters for CascadeMetaForItem and better progress implementation
      Change 1.0.3: 2019-03-01; made compatible for Photo Supreme
      Change 1.0.2: 2014-02-21; made compatible for Photo Supreme

 Description:

 This version will cascade the meta data to the entire version stack. The script will always take the main version for every selected
 image and then cascade the main version's meta-data to every sub-version in the version stack. Beware that this overwrites the sub
 version's meta-data (Exif, IPTC and XMP) and replace it with the main version's data
 image and then cascade the main version's meta-data to every sub-version in the version stack. Beware that this overwrites the sub
 version's meta-data (Exif, IPTC and XMP) and replace it with the main version's data
}

function InternalCascadeMetaForItem(AItem: TCatalogItem): Boolean;
var
  AVersions: TCatalogItems;
  AMain: TCatalogItem;
  ATypes: TCatalogCascadeTypes;
begin
  result := False;
  AVersions := TCatalogItems.Create(TCatalogItem, '');
  AMain := TCatalogItem.Create(nil);
  try
    if not PublicCatalog.FindItemCombined2(AItem, AMain, True, vptNone) then
      exit;

    // make sure we have the latest info
    PublicCatalog.FindItemCombined2(AItem, AItem, False, vptNone);
    // enum the version stack
    PublicCatalog.EnumVersionsForItem(AMain, AVersions);

    // all but orientation and cataloglabels
    ATypes := [cctMeta, cctRatings, cctLabels, cctXMP, cctXMPTechnical, cctGEOCoordinates];
    PublicCatalog.CascadeToTargetsForItem(AItem, AVersions, ATypes, PublicOptions);
    result := True;
  finally
    AVersions.Free;
    AMain.Free;
  end;
end;

var
  i: Integer;
  AMain: TCatalogItem;
  AProgress: TxomProgress;
begin
  if not Ask ('Are you sure you want to cascade the meta data to the entire version stack based on the meta data for the main version of the selected images?' + CrLf2W + 'Beware; This is permanent and is not undoable!!' + CrLf2W) then
    exit;

  AProgress := TxomProgress.Create(nil);
  AProgress.Max := Selected.Count;
  AProgress.Pos := 0;

  AProgress.Show;

  for i := 0 to Selected.Count - 1 do
  begin
    AProgress.Pos := i + 1;
    AProgress.ProgressText := Selected.Items[i].FileNameOnly;

    AMain := TCatalogItem.Create(nil);
    try
      if PublicCatalog.FindImageCombined(Selected.Items[i], AMain, True, phtNone) then    // make sure we find the main version for the selected image
        InternalCascadeMetaForItem(AMain);                       // cascade the main version's meta data to all sub versions
    finally
      AMain.Free;
    end;

    if AProgress.Cancel then
      break;
  end;

  AProgress.Hide;

  if AProgress.Cancel then
    Say ('Cancelled')

  AProgress.Free;
end;
