'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.tagReaders = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _swfBuffer = require('./swf-buffer');

var _swfTags = require('./swf-tags');

/*
   for parsing from rawData of tags
   which is the sliced version whose content starts
   at index 0
*/
var tagReaders = {};

var define = function define(code, reader) {
  tagReaders[code] = function (buffer) {
    return _extends({
      code: code
    }, reader(buffer));
  };
};

define(_swfTags.SwfTags.DefineBits, function (buffer) {
  var buff = new _swfBuffer.SWFBuffer(buffer);
  var length = buffer.length;
  var characterId = buff.readUIntLE(16);
  var jpegData = buff.buffer.slice(buff.pointer, buff.pointer + length - 2);
  buff.incr(length - 2);
  return {
    characterId: characterId,
    jpegData: jpegData
  };
});

define(_swfTags.SwfTags.JPEGTables, function (buffer) {
  var buff = new _swfBuffer.SWFBuffer(buffer);
  var length = buffer.length;
  var jpegData = buff.buffer.slice(buff.pointer, buff.pointer + length);
  buff.incr(length);
  return {
    jpegData: jpegData
  };
});

define(_swfTags.SwfTags.DefineBitsJPEG2, function (buffer) {
  var buff = new _swfBuffer.SWFBuffer(buffer);
  var length = buffer.length;
  var characterId = buff.readUIntLE(16);
  var imageData = buff.buffer.slice(buff.pointer, buff.pointer + length - 2);
  buff.incr(length - 2);
  return {
    characterId: characterId,
    imageData: imageData
  };
});

define(_swfTags.SwfTags.DefineBitsJPEG3, function (buffer) {
  var buff = new _swfBuffer.SWFBuffer(buffer);
  var length = buffer.length;
  var characterId = buff.readUIntLE(16);
  var alphaDataOffset = buff.readUIntLE(32);
  var imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
  buff.incr(alphaDataOffset);
  var restLength = length - 6 - alphaDataOffset;
  var bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
  buff.incr(restLength);
  return {
    characterId: characterId,
    alphaDataOffset: alphaDataOffset,
    imageData: imageData,
    bitmapAlphaData: bitmapAlphaData
  };
});

var gDefineBitsLosslessHandler = function gDefineBitsLosslessHandler(code) {
  return function (buffer) {
    var buff = new _swfBuffer.SWFBuffer(buffer);
    var length = buffer.length;
    var characterId = buff.readUIntLE(16);
    /*
       bitmapFormat:
       - 3: 8-bit colormapped image
       - 4: 15-bit RGB image (no such value for DefineBitsLossless2)
       - 5: 32-bit ARGB image
     */
    var bitmapFormat = buff.readUInt8();
    var bitmapWidth = buff.readUIntLE(16);
    var bitmapHeight = buff.readUIntLE(16);
    var bitmapColorTableSize = null;
    var restLength = length - 7;
    if (bitmapFormat === 3) {
      bitmapColorTableSize = buff.readUInt8();
      --restLength;
    }
    var zlibBitmapData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
    buff.incr(restLength);
    return {
      code: code,
      characterId: characterId,
      bitmapFormat: bitmapFormat,
      bitmapWidth: bitmapWidth, bitmapHeight: bitmapHeight,
      bitmapColorTableSize: bitmapColorTableSize,
      zlibBitmapData: zlibBitmapData
    };
  };
};

tagReaders[_swfTags.SwfTags.DefineBitsLossless] = gDefineBitsLosslessHandler(_swfTags.SwfTags.DefineBitsLossless);

tagReaders[_swfTags.SwfTags.DefineBitsLossless2] = gDefineBitsLosslessHandler(_swfTags.SwfTags.DefineBitsLossless2);

define(_swfTags.SwfTags.DefineBitsJPEG4, function (buffer) {
  var buff = new _swfBuffer.SWFBuffer(buffer);
  var length = buffer.length;
  var characterId = buff.readUIntLE(16);
  var alphaDataOffset = buff.readUIntLE(32);
  var deblockParam = buff.readUIntLE(16);
  var imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
  buff.incr(alphaDataOffset);
  var restLength = length - 8 - alphaDataOffset;
  var bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
  buff.incr(restLength);
  return {
    characterId: characterId,
    alphaDataOffset: alphaDataOffset,
    deblockParam: deblockParam,
    imageData: imageData,
    bitmapAlphaData: bitmapAlphaData
  };
});

exports.tagReaders = tagReaders;