import { createReader, readFloat32, readSignature, readUint16, readUint32, readUnicodeStringWithLength } from './psdReader';
export function readAse(buffer) {
    var reader = createReader(buffer.buffer, buffer.byteOffset, buffer.byteLength);
    var signature = readSignature(reader); // ASEF
    if (signature !== 'ASEF')
        throw new Error('Invalid signature');
    var versionMajor = readUint16(reader); // 1
    var versionMinor = readUint16(reader); // 0
    if (versionMajor !== 1 || versionMinor !== 0)
        throw new Error('Invalid version');
    var blocksCount = readUint32(reader);
    var colorTypes = ['global', 'spot', 'normal'];
    var ase = { colors: [] };
    var group = ase;
    for (var i = 0; i < blocksCount; i++) {
        var type = readUint16(reader);
        var length_1 = readUint32(reader);
        var end = reader.offset + length_1;
        switch (type) {
            case 0x0001: { // color
                var nameLength = readUint16(reader);
                var name_1 = readUnicodeStringWithLength(reader, nameLength);
                var colorMode = readSignature(reader);
                var color = void 0;
                switch (colorMode) {
                    case 'RGB ':
                        color = {
                            r: readFloat32(reader),
                            g: readFloat32(reader),
                            b: readFloat32(reader),
                            type: colorTypes[readUint16(reader)],
                        };
                        break;
                    case 'CMYK':
                        color = {
                            c: readFloat32(reader),
                            m: readFloat32(reader),
                            y: readFloat32(reader),
                            k: readFloat32(reader),
                            type: colorTypes[readUint16(reader)],
                        };
                        break;
                    case 'Gray':
                        color = {
                            k: readFloat32(reader),
                            type: colorTypes[readUint16(reader)],
                        };
                        break;
                    case 'LAB ':
                        color = {
                            l: readFloat32(reader),
                            a: readFloat32(reader),
                            b: readFloat32(reader),
                            type: colorTypes[readUint16(reader)],
                        };
                        break;
                    default:
                        throw new Error('Invalid color mode');
                }
                group.colors.push({ name: name_1, color: color });
                break;
            }
            case 0xC001: { // group start
                var nameLength = readUint16(reader);
                var name_2 = readUnicodeStringWithLength(reader, nameLength);
                ase.colors.push(group = { name: name_2, colors: [] });
                break;
            }
            case 0xC002: // group end
                group = ase;
                break;
            default:
                throw new Error('Invalid block type');
        }
        reader.offset = end;
    }
    return ase;
}
//# sourceMappingURL=ase.js.map