2025-07-23 11:25:55 +01:00
|
|
|
import { useState } from 'react';
|
|
|
|
import {
|
|
|
|
COLOR_TYPES,
|
|
|
|
OUTPUT_OPTIONS,
|
|
|
|
TO_FORMAT_OPTIONS,
|
|
|
|
CONVERSION_MATRIX,
|
|
|
|
type ColorType,
|
2025-07-23 17:23:25 +01:00
|
|
|
type OutputOption
|
2025-07-23 11:25:55 +01:00
|
|
|
} from '../../../constants/convertConstants';
|
2025-07-23 17:23:25 +01:00
|
|
|
import { getEndpointName as getEndpointNameUtil, getEndpointUrl } from '../../../utils/convertUtils';
|
2025-07-23 11:25:55 +01:00
|
|
|
|
|
|
|
export interface ConvertParameters {
|
|
|
|
fromExtension: string;
|
|
|
|
toExtension: string;
|
2025-07-28 13:58:43 +01:00
|
|
|
pageNumbers: string;
|
2025-07-23 11:25:55 +01:00
|
|
|
imageOptions: {
|
|
|
|
colorType: ColorType;
|
|
|
|
dpi: number;
|
|
|
|
singleOrMultiple: OutputOption;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ConvertParametersHook {
|
|
|
|
parameters: ConvertParameters;
|
|
|
|
updateParameter: (parameter: keyof ConvertParameters, value: any) => void;
|
|
|
|
resetParameters: () => void;
|
|
|
|
validateParameters: () => boolean;
|
|
|
|
getEndpointName: () => string;
|
|
|
|
getEndpoint: () => string;
|
|
|
|
getAvailableToExtensions: (fromExtension: string) => Array<{value: string, label: string, group: string}>;
|
|
|
|
detectFileExtension: (filename: string) => string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialParameters: ConvertParameters = {
|
|
|
|
fromExtension: '',
|
|
|
|
toExtension: '',
|
2025-07-28 13:58:43 +01:00
|
|
|
pageNumbers: 'all',
|
2025-07-23 11:25:55 +01:00
|
|
|
imageOptions: {
|
|
|
|
colorType: COLOR_TYPES.COLOR,
|
|
|
|
dpi: 300,
|
|
|
|
singleOrMultiple: OUTPUT_OPTIONS.MULTIPLE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useConvertParameters = (): ConvertParametersHook => {
|
|
|
|
const [parameters, setParameters] = useState<ConvertParameters>(initialParameters);
|
|
|
|
|
|
|
|
const updateParameter = (parameter: keyof ConvertParameters, value: any) => {
|
|
|
|
setParameters(prev => ({ ...prev, [parameter]: value }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetParameters = () => {
|
|
|
|
setParameters(initialParameters);
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateParameters = () => {
|
|
|
|
const { fromExtension, toExtension } = parameters;
|
|
|
|
|
|
|
|
if (!fromExtension || !toExtension) return false;
|
|
|
|
|
|
|
|
// Check if conversion is supported
|
|
|
|
const supportedToExtensions = CONVERSION_MATRIX[fromExtension];
|
|
|
|
if (!supportedToExtensions || !supportedToExtensions.includes(toExtension)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional validation for image conversions
|
|
|
|
if (['png', 'jpg'].includes(toExtension)) {
|
|
|
|
return parameters.imageOptions.dpi >= 72 && parameters.imageOptions.dpi <= 600;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getEndpointName = () => {
|
|
|
|
const { fromExtension, toExtension } = parameters;
|
2025-07-23 17:23:25 +01:00
|
|
|
return getEndpointNameUtil(fromExtension, toExtension);
|
2025-07-23 11:25:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const getEndpoint = () => {
|
2025-07-23 17:23:25 +01:00
|
|
|
const { fromExtension, toExtension } = parameters;
|
|
|
|
return getEndpointUrl(fromExtension, toExtension);
|
2025-07-23 11:25:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const getAvailableToExtensions = (fromExtension: string) => {
|
|
|
|
if (!fromExtension) return [];
|
|
|
|
|
|
|
|
const supportedExtensions = CONVERSION_MATRIX[fromExtension] || [];
|
|
|
|
return TO_FORMAT_OPTIONS.filter(option =>
|
|
|
|
supportedExtensions.includes(option.value)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const detectFileExtension = (filename: string): string => {
|
|
|
|
const extension = filename.split('.').pop()?.toLowerCase();
|
|
|
|
return extension || '';
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
parameters,
|
|
|
|
updateParameter,
|
|
|
|
resetParameters,
|
|
|
|
validateParameters,
|
|
|
|
getEndpointName,
|
|
|
|
getEndpoint,
|
|
|
|
getAvailableToExtensions,
|
|
|
|
detectFileExtension,
|
|
|
|
};
|
|
|
|
};
|