"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssetEditsResponseDto = exports.AssetEditsCreateDto = exports.AssetEditActionItemResponseDto = exports.MirrorAxis = exports.AssetEditActionSchema = exports.AssetEditAction = void 0;
const openapi = require("@nestjs/swagger");
const nestjs_zod_1 = require("nestjs-zod");
const zod_1 = __importDefault(require("zod"));
var AssetEditAction;
(function (AssetEditAction) {
    AssetEditAction["Crop"] = "crop";
    AssetEditAction["Rotate"] = "rotate";
    AssetEditAction["Mirror"] = "mirror";
})(AssetEditAction || (exports.AssetEditAction = AssetEditAction = {}));
exports.AssetEditActionSchema = zod_1.default
    .enum(AssetEditAction)
    .describe('Type of edit action to perform')
    .meta({ id: 'AssetEditAction' });
var MirrorAxis;
(function (MirrorAxis) {
    MirrorAxis["Horizontal"] = "horizontal";
    MirrorAxis["Vertical"] = "vertical";
})(MirrorAxis || (exports.MirrorAxis = MirrorAxis = {}));
const MirrorAxisSchema = zod_1.default.enum(['horizontal', 'vertical']).describe('Axis to mirror along').meta({ id: 'MirrorAxis' });
const CropParametersSchema = zod_1.default
    .object({
    x: zod_1.default.int().min(0).describe('Top-Left X coordinate of crop'),
    y: zod_1.default.int().min(0).describe('Top-Left Y coordinate of crop'),
    width: zod_1.default.int().min(1).describe('Width of the crop'),
    height: zod_1.default.int().min(1).describe('Height of the crop'),
})
    .meta({ id: 'CropParameters' });
const RotateParametersSchema = zod_1.default
    .object({
    angle: zod_1.default
        .number()
        .refine((v) => [0, 90, 180, 270].includes(v), {
        error: 'Angle must be one of the following values: 0, 90, 180, 270',
    })
        .describe('Rotation angle in degrees'),
})
    .meta({ id: 'RotateParameters' });
const MirrorParametersSchema = zod_1.default
    .object({
    axis: MirrorAxisSchema,
})
    .meta({ id: 'MirrorParameters' });
const __AssetEditActionItemSchema = zod_1.default.discriminatedUnion('action', [
    zod_1.default.object({ action: exports.AssetEditActionSchema.extract(['Crop']), parameters: CropParametersSchema }),
    zod_1.default.object({ action: exports.AssetEditActionSchema.extract(['Rotate']), parameters: RotateParametersSchema }),
    zod_1.default.object({ action: exports.AssetEditActionSchema.extract(['Mirror']), parameters: MirrorParametersSchema }),
]);
const AssetEditParametersSchema = zod_1.default
    .union([CropParametersSchema, RotateParametersSchema, MirrorParametersSchema], {
    error: getExpectedKeysByActionMessage,
})
    .describe('List of edit actions to apply (crop, rotate, or mirror)');
const actionParameterMap = {
    [AssetEditAction.Crop]: CropParametersSchema,
    [AssetEditAction.Rotate]: RotateParametersSchema,
    [AssetEditAction.Mirror]: MirrorParametersSchema,
};
function getExpectedKeysByActionMessage() {
    const expectedByAction = Object.entries(actionParameterMap)
        .map(([action, schema]) => `${action}: [${Object.keys(schema.shape).join(', ')}]`)
        .join('; ');
    return `Invalid parameters for action, expected keys by action: ${expectedByAction}`;
}
function isParametersValidForAction(edit) {
    return actionParameterMap[edit.action].safeParse(edit.parameters).success;
}
const AssetEditActionItemSchema = zod_1.default
    .object({
    action: exports.AssetEditActionSchema,
    parameters: AssetEditParametersSchema,
})
    .superRefine((edit, ctx) => {
    if (!isParametersValidForAction(edit)) {
        ctx.addIssue({
            code: 'custom',
            path: ['parameters'],
            message: `Invalid parameters for action '${edit.action}', expecting keys: ${Object.keys(actionParameterMap[edit.action].shape).join(', ')}`,
        });
    }
})
    .meta({ id: 'AssetEditActionItemDto' });
function uniqueEditActions(edits) {
    const keys = new Set();
    for (const edit of edits) {
        const key = edit.action === 'mirror' ? `mirror-${JSON.stringify(edit.parameters)}` : edit.action;
        if (keys.has(key)) {
            return false;
        }
        keys.add(key);
    }
    return true;
}
const AssetEditsCreateSchema = zod_1.default
    .object({
    edits: zod_1.default
        .array(AssetEditActionItemSchema)
        .min(1)
        .describe('List of edit actions to apply (crop, rotate, or mirror)')
        .refine(uniqueEditActions, { error: 'Duplicate edit actions are not allowed' }),
})
    .meta({ id: 'AssetEditsCreateDto' });
const AssetEditActionItemResponseSchema = AssetEditActionItemSchema.extend({
    id: zod_1.default.uuidv4().describe('Asset edit ID'),
}).meta({ id: 'AssetEditActionItemResponseDto' });
const AssetEditsResponseSchema = zod_1.default
    .object({
    assetId: zod_1.default.uuidv4().describe('Asset ID these edits belong to'),
    edits: zod_1.default.array(AssetEditActionItemResponseSchema).describe('List of edit actions applied to the asset'),
})
    .meta({ id: 'AssetEditsResponseDto' });
class AssetEditActionItemResponseDto extends (0, nestjs_zod_1.createZodDto)(AssetEditActionItemResponseSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.AssetEditActionItemResponseDto = AssetEditActionItemResponseDto;
class AssetEditsCreateDto extends (0, nestjs_zod_1.createZodDto)(AssetEditsCreateSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.AssetEditsCreateDto = AssetEditsCreateDto;
class AssetEditsResponseDto extends (0, nestjs_zod_1.createZodDto)(AssetEditsResponseSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.AssetEditsResponseDto = AssetEditsResponseDto;
//# sourceMappingURL=editing.dto.js.map