{
  this script will search for catalog labels that have the "Mapped To" field filled in"

  author: H van Zwietering
  initial date: 2022-11-16
}

var
  AProps: TCatalogItemProps;
  AProp: TCatalogItemProp;
  i: Integer;
  AList: TTntStringList;
  ASl: TTntStringList;
  AResult: Integer;
  ACheck: Boolean;
  ADoAsk: Boolean;
  AHit: Integer;
  AProgress: TxomProgress;
begin
  AProgress := TxomProgress.Create(nil);
  AProgress.Cancel := False;

  AProps := TCatalogItemProps.Create(TCatalogItemProp, '');
  AList := TTntStringList.Create;

  AProgress.Caption := 'Finding catalog labels with mapping...';
  AProgress.Show;

  PublicCatalog.EnumAllProps(AProps);

  AProgress.Max := AProps.Count;
  AProgress.Pos := 0;

  for i := 0 to AProps.Count - 1 do
  begin
    AProgress.Pos := i + 1;
    AProgress.ProgressText := IntToStr(MulDiv (i, 100, AProps.Count)) + '%; Found ' + IntToStr(AList.Count) + ' catalog labels';

    AProp := AProps.Items[i];
    if AProp.PropValue = AutoPropValue then    // skip auto-props
      Continue;

    if AProp.PropXMPLink <> '' then
      AList.AddObject (PublicCatalog.PathNameForProp(AProp, '::', True), AProp);

    // for testing
    //if AList.Count = 100 then
    //  break;

    if AProgress.Cancel then
      break;
  end;

  Progress.Hide;

  ASaved := False;
  ASl := TTntStringList.Create;
  ASl.Add('Save results to file');
  ASl.Add('Show results on-screen');
  ASl.Add('Delete all ' + IntToStr(AList.Count) + ' catalog labels');
  ASl.Add('Cancel');

  ADoAsk := AList.Count > 0;
  if not ADoAsk then
    Say('No results');
  while ADoAsk do
  begin
    ACheck := False;
    AResult := AskCustom ('What would you like to do?', 'There are ' + IntToStr(AList.Count) + ' catalog labels found with "Mapped To" filled in.', '', '', ACheck, ASl);

    case AResult of
      0:      // save to file
      begin
        SaveDialog.FileName := WideIncludeTrailingBackslash(WindowsDesktopDir) + 'LabelsWithMapping.txt';
        if SaveDialog.Execute then
        begin
          AList.SaveToFile (SaveDialog.FileName);
          Say ('The results are saved to file ' + SaveDialog.FileName);
          ADoAsk := False;
        end;
      end;
      1:      // show on screen
      begin
        Say (AList.Text);
        ADoAsk := True;
      end;
      2:      // delete empty labels
      begin
        if Ask ('Are you sure you want to permanently delete these ' + IntToStr(AList.Count) + ' catalog labels?') then
        begin
          AProgress.Cancel := False;
          AProgress.Max := AList.Count;
          AProgress.Pos := 0;
          AProgress.Show;

          AHit := 0;
          for i := 0 to AList.Count - 1 do
          begin
            AProgress.Pos := i + i;
            AProgress.ProgressText := IntToStr(MulDiv (i, 100, AList.Count)) + ' %';

            AProp := AList.Objects[i];

            //Say (AProp.PropName);
            Catalog.DeleteProp (AProp);
            Inc(AHit);

            if AProgress.Cancel then
              break;
          end;

          AProgress.Hide;

          Say (IntToStr(AHit) + ' catalog labels are deleted from the catalog');
          ADoAsk := not AProgress.Cancel;
        end;
      end;
      -1, 3:      // cancel
      begin
        ADoAsk := False;
      end;
    end;
  end;

  AList.Free;
  AProps.Free;

  AProgress.Free;
end;



