"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateDeviceUseCase = void 0;
const uuid_1 = require("uuid");
const device_1 = require("../../../domain/entities/device");
class CreateDeviceUseCase {
    deviceRepository;
    constructor(deviceRepository) {
        this.deviceRepository = deviceRepository;
    }
    async execute(input) {
        const existing = await this.deviceRepository.findByFingerprint(input.fingerprint);
        if (existing) {
            const pushToken = input.pushToken ?? null;
            if (pushToken && existing.pushToken !== pushToken) {
                const updated = existing.updatePushToken(pushToken);
                return this.deviceRepository.update(updated);
            }
            return existing;
        }
        const device = device_1.Device.create((0, uuid_1.v4)(), input.location, input.type, input.info ?? null, input.ipAddress, input.userAgent ?? null, input.fingerprint, input.status, input.pushToken ?? null);
        return this.deviceRepository.save(device);
    }
}
exports.CreateDeviceUseCase = CreateDeviceUseCase;
//# sourceMappingURL=create-device.use-case.js.map