Repository: Stirling-Tools/Stirling-PDF
File: frontend/src/core/hooks/tools/addWatermark/useAddWatermarkOperation.ts (TypeScript)

Code before:
import { useTranslation } from 'react-i18next';
import { ToolType, useToolOperation } from '@app/hooks/tools/shared/useToolOperation';
import { createStandardErrorHandler } from '@app/utils/toolErrorHandler';
import { AddWatermarkParameters, defaultParameters } from '@app/hooks/tools/addWatermark/useAddWatermarkParameters';

// Static function that can be used by both the hook and automation executor
export const buildAddWatermarkFormData = (parameters: AddWatermarkParameters, file: File): FormData => {
  const formData = new FormData();
  formData.append("fileInput", file);

  // Required: watermarkType as string
  formData.append("watermarkType", parameters.watermarkType || "text");

  // Add watermark content based on type
  if (parameters.watermarkType === 'text') {
    formData.append("watermarkText", parameters.watermarkText);
  } else if (parameters.watermarkType === 'image' && parameters.watermarkImage) {
    formData.append("watermarkImage", parameters.watermarkImage);
  }

  // Required parameters with correct formatting
  formData.append("fontSize", (parameters.fontSize ?? 0).toString());
  formData.append("rotation", (parameters.rotation ?? 0).toString());
  formData.append("opacity", ((parameters.opacity ?? 0) / 100).toString()); // Convert percentage to decimal
  formData.append("widthSpacer", (parameters.widthSpacer ?? 0).toString());
  formData.append("heightSpacer", (parameters.heightSpacer ?? 0).toString());

  // Backend-expected parameters from user input
  formData.append("alphabet", parameters.alphabet || "");
  formData.append("customColor", parameters.customColor || "");
  formData.append("convertPDFToImage", (parameters.convertPDFToImage ?? false).toString());

  return formData;
};

// Static configuration object
export const addWatermarkOperationConfig = {
  toolType: ToolType.singleFile,
  buildFormData: buildAddWatermarkFormData,
  operationType: 'watermark',
  endpoint: '/api/v1/security/add-watermark',
  defaultParameters,
} as const;

export const useAddWatermarkOperation = () => {
  const { t } = useTranslation();

  return useToolOperation<AddWatermarkParameters>({
    ...addWatermarkOperationConfig,
    getErrorMessage: createStandardErrorHandler(t('watermark.error.failed', 'An error occurred while adding watermark to the PDF.'))
  });

Review comment (suggestion):
I'm not entirely convinced this is the right way to fix this. In theory, the type system should guarantee that none of the options in `AddWartermarkParameters` are optional, so using `?? 0` should really be detected as dead code. Marking the input to the function as `parameters: Partial<AddWatermarkParameters>` would make the defaults necessary, so maybe that's the way forward here, but I suspect this is just being caused by some bad casting in Automate which should be starting out with `defaultParameters` instead of an empty object, or something along those lines. Where exactly is it that we can end up in a situation where these aren't defined?

Also, if we are needing to provide defaults, are we better off using like:

```typescript
  formData.append("fontSize", (parameters.fontSize ?? defaultParameters.fontSize).toString());
```

Code after:
import { useTranslation } from 'react-i18next';
import { ToolType, useToolOperation } from '@app/hooks/tools/shared/useToolOperation';
import { createStandardErrorHandler } from '@app/utils/toolErrorHandler';
import { AddWatermarkParameters, defaultParameters } from '@app/hooks/tools/addWatermark/useAddWatermarkParameters';

// Static function that can be used by both the hook and automation executor
export const buildAddWatermarkFormData = (parameters: AddWatermarkParameters, file: File): FormData => {
  const formData = new FormData();
  formData.append("fileInput", file);

  // Required: watermarkType as string
  formData.append("watermarkType", parameters.watermarkType || "text");

  // Add watermark content based on type
  if (parameters.watermarkType === 'text') {
    formData.append("watermarkText", parameters.watermarkText);
  } else if (parameters.watermarkType === 'image' && parameters.watermarkImage) {
    formData.append("watermarkImage", parameters.watermarkImage);
  }

  // Required parameters with correct formatting (defaults merged in automationExecutor)
  formData.append("fontSize", parameters.fontSize.toString());
  formData.append("rotation", parameters.rotation.toString());
  formData.append("opacity", (parameters.opacity / 100).toString()); // Convert percentage to decimal
  formData.append("widthSpacer", parameters.widthSpacer.toString());
  formData.append("heightSpacer", parameters.heightSpacer.toString());

  // Backend-expected parameters from user input
  formData.append("alphabet", parameters.alphabet || "");
  formData.append("customColor", parameters.customColor || "");
  formData.append("convertPDFToImage", (parameters.convertPDFToImage ?? false).toString());

  return formData;
};

// Static configuration object
export const addWatermarkOperationConfig = {
  toolType: ToolType.singleFile,
  buildFormData: buildAddWatermarkFormData,
  operationType: 'watermark',
  endpoint: '/api/v1/security/add-watermark',
  defaultParameters,
} as const;

export const useAddWatermarkOperation = () => {
  const { t } = useTranslation();

  return useToolOperation<AddWatermarkParameters>({
    ...addWatermarkOperationConfig,
    getErrorMessage: createStandardErrorHandler(t('watermark.error.failed', 'An error occurred while adding watermark to the PDF.'))
  });