"use strict";
var _DataTransferUploadClip_clipIndex, _DataTransferUploadClip_name, _DataTransferUploadClip_frames, _DataTransferUploadClip_nextTransferId, _DataTransferUploadClip_currentFrame, _DataTransferUploadClip_numFrames, _DataTransferUploadClipFrame_clipIndex, _DataTransferUploadClipFrame_frameIndex, _DataTransferUploadClipFrame_dataLength;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTransferUploadClipFrame = exports.DataTransferUploadClip = void 0;
const tslib_1 = require("tslib");
const DataTransfer_1 = require("../commands/DataTransfer");
const dataTransfer_1 = require("./dataTransfer");
const Media_1 = require("../commands/Media");
const debug0 = require("debug");
const dataTransferUploadBuffer_1 = require("./dataTransferUploadBuffer");
const debug = debug0('atem-connection:data-transfer:upload-clip');
class DataTransferUploadClip extends dataTransfer_1.DataTransfer {
    constructor(clipIndex, name, frames, nextTransferId) {
        super();
        _DataTransferUploadClip_clipIndex.set(this, void 0);
        _DataTransferUploadClip_name.set(this, void 0);
        _DataTransferUploadClip_frames.set(this, void 0);
        _DataTransferUploadClip_nextTransferId.set(this, void 0);
        _DataTransferUploadClip_currentFrame.set(this, void 0);
        _DataTransferUploadClip_numFrames.set(this, 0);
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_clipIndex, clipIndex, "f");
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_name, name, "f");
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_frames, frames, "f");
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_nextTransferId, nextTransferId, "f");
    }
    async nextFrame() {
        const next = await tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_frames, "f").next();
        return next.value;
    }
    async startTransfer(transferId) {
        var _a;
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_currentFrame, await this.nextFrame(), "f");
        if (!tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f")) {
            throw new Error('No frames available for transfer');
        }
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_numFrames, (_a = tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_numFrames, "f"), _a++, _a), "f");
        const frameResult = await tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f").startTransfer(transferId);
        return {
            newState: dataTransfer_1.DataTransferState.Ready,
            commands: [new Media_1.MediaPoolClearClipCommand(tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_clipIndex, "f")), ...frameResult.commands],
        };
    }
    /** Restart the current transfer */
    async restartTransfer(transferId) {
        if (tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f")) {
            return tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f").startTransfer(transferId);
        }
        else {
            // TODO - is this correct?
            return this.startTransfer(transferId);
        }
    }
    async handleCommand(command, oldState) {
        var _a;
        if (!tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f")) {
            throw new Error('No frames available for transfer');
        }
        // TODO - is oldState appropriate here?
        const frameResult = await tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f").handleCommand(command, oldState);
        debug(`handleCommand: ${dataTransfer_1.DataTransferState[frameResult.newState]} - Giving ${frameResult.commands.length} commands`);
        if (frameResult.newState === dataTransfer_1.DataTransferState.Finished) {
            // Get the next frame
            tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_currentFrame, await this.nextFrame(), "f");
            debug(`Start next frame (${!!tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f")})`);
            if (tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f")) {
                tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClip_numFrames, (_a = tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_numFrames, "f"), _a++, _a), "f");
                const newId = tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_nextTransferId, "f").call(this);
                const newFrameResult = await tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_currentFrame, "f").startTransfer(newId);
                return {
                    newState: dataTransfer_1.DataTransferState.Ready,
                    newId: newId,
                    commands: [...frameResult.commands, ...newFrameResult.commands],
                };
            }
            else {
                // Looks like we finished
                this.resolvePromise();
                debug(`Clip complete`);
                return {
                    newState: dataTransfer_1.DataTransferState.Finished,
                    commands: [
                        ...frameResult.commands,
                        new Media_1.MediaPoolSetClipCommand({
                            index: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_clipIndex, "f"),
                            name: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_name, "f"),
                            frames: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClip_numFrames, "f"),
                        }),
                    ],
                };
            }
        }
        else {
            return {
                newState: dataTransfer_1.DataTransferState.Transferring,
                commands: frameResult.commands,
            };
        }
    }
}
exports.DataTransferUploadClip = DataTransferUploadClip;
_DataTransferUploadClip_clipIndex = new WeakMap(), _DataTransferUploadClip_name = new WeakMap(), _DataTransferUploadClip_frames = new WeakMap(), _DataTransferUploadClip_nextTransferId = new WeakMap(), _DataTransferUploadClip_currentFrame = new WeakMap(), _DataTransferUploadClip_numFrames = new WeakMap();
class DataTransferUploadClipFrame extends dataTransferUploadBuffer_1.DataTransferUploadBuffer {
    constructor(clipIndex, frameIndex, buffer) {
        super(buffer);
        _DataTransferUploadClipFrame_clipIndex.set(this, void 0);
        _DataTransferUploadClipFrame_frameIndex.set(this, void 0);
        _DataTransferUploadClipFrame_dataLength.set(this, void 0);
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClipFrame_clipIndex, clipIndex, "f");
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClipFrame_frameIndex, frameIndex, "f");
        tslib_1.__classPrivateFieldSet(this, _DataTransferUploadClipFrame_dataLength, buffer.rawDataLength, "f");
    }
    async startTransfer(transferId) {
        debug(`Start transfer ${transferId} (${tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClipFrame_dataLength, "f")})`);
        const command = new DataTransfer_1.DataTransferUploadRequestCommand({
            transferId: transferId,
            transferStoreId: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClipFrame_clipIndex, "f") + 1,
            transferIndex: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClipFrame_frameIndex, "f"),
            size: tslib_1.__classPrivateFieldGet(this, _DataTransferUploadClipFrame_dataLength, "f"),
            mode: 1,
        });
        return {
            newState: dataTransfer_1.DataTransferState.Ready,
            commands: [command],
        };
    }
    generateDescriptionCommand(transferId) {
        debug(`Generate frame description for transfer ${transferId}`);
        return new DataTransfer_1.DataTransferFileDescriptionCommand({
            name: undefined,
            description: undefined,
            fileHash: this.hash,
            transferId: transferId,
        });
    }
}
exports.DataTransferUploadClipFrame = DataTransferUploadClipFrame;
_DataTransferUploadClipFrame_clipIndex = new WeakMap(), _DataTransferUploadClipFrame_frameIndex = new WeakMap(), _DataTransferUploadClipFrame_dataLength = new WeakMap();
//# sourceMappingURL=dataTransferUploadClip.js.map