var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { _defineLazyProperty, _mathClamp, _PdfDictionary, _PdfName, _PdfReference } from '@syncfusion/ej2-pdf';
import { _PdfAlternateCS, _PdfCalGrayCS, _PdfColorRgbConverter, _PdfDeviceCmykCS, _PdfDeviceGrayCS, _PdfDeviceRgbaCS, _PdfDeviceRgbCS, _PdfIndexedCS, _PdfLabCS, _PdfPatternCS } from './colorspace';
import { _PdfIccColorSpace } from './icc-based-colorspace';
var _PdfColorSpaceUtils = /** @class */ (function () {
    function _PdfColorSpaceUtils(callback) {
        this._canvasRenderCallback = callback;
    }
    _PdfColorSpaceUtils.prototype._parse = function (colorSpace, xref, resources, pdfFunctionFactory, // eslint-disable-line
    globalColorSpaceCache, localColorSpaceCache, asyncIfNotCached) {
        return __awaiter(this, void 0, void 0, function () {
            var options, csRef, cachedCS, parsedCS;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        options = { xref: xref, resources: resources, pdfFunctionFactory: pdfFunctionFactory, globalColorSpaceCache: globalColorSpaceCache, localColorSpaceCache: localColorSpaceCache };
                        if (colorSpace instanceof _PdfReference) {
                            csRef = colorSpace;
                            cachedCS = globalColorSpaceCache && globalColorSpaceCache.get(csRef) ||
                                localColorSpaceCache && localColorSpaceCache.get(csRef);
                            if (cachedCS) {
                                return [2 /*return*/, cachedCS];
                            }
                            colorSpace = xref._fetch(colorSpace);
                        }
                        return [4 /*yield*/, this._parseColorspace(colorSpace, options)];
                    case 1:
                        parsedCS = _a.sent();
                        return [2 /*return*/, asyncIfNotCached ? Promise.resolve(parsedCS) : parsedCS];
                }
            });
        });
    };
    _PdfColorSpaceUtils.prototype._subParse = function (cs, options) {
        return __awaiter(this, void 0, void 0, function () {
            var globalColorSpaceCache, csRef, cachedCS, parsedCS;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        globalColorSpaceCache = options.globalColorSpaceCache;
                        if (globalColorSpaceCache !== null && typeof (globalColorSpaceCache) !== 'undefined' && cs instanceof _PdfReference) {
                            csRef = cs;
                            cachedCS = globalColorSpaceCache.get(csRef);
                            if (cachedCS) {
                                return [2 /*return*/, cachedCS];
                            }
                        }
                        return [4 /*yield*/, this._parseColorspace(cs, options)];
                    case 1:
                        parsedCS = _a.sent();
                        if (csRef) {
                            globalColorSpaceCache.set(null, csRef, parsedCS);
                        }
                        return [2 /*return*/, parsedCS];
                }
            });
        });
    };
    _PdfColorSpaceUtils.prototype._parseColorspace = function (cs, options) {
        return __awaiter(this, void 0, void 0, function () {
            var xref, resources, _a, colorSpaces, resourcesCS, mode, params, numComps, baseCS, whitePoint, blackPoint, gamma, matrix, hiVal, lookup, name_1, range, stream, dictionary, altRaw, altCS, _b, colorSpace, ex_1;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        xref = options.xref, resources = options.resources;
                        if (cs instanceof _PdfReference) {
                            cs = xref._fetch(cs);
                        }
                        if (!(cs instanceof _PdfName)) return [3 /*break*/, 10];
                        _a = cs.name;
                        switch (_a) {
                            case 'G': return [3 /*break*/, 1];
                            case 'DeviceGray': return [3 /*break*/, 1];
                            case 'RGB': return [3 /*break*/, 2];
                            case 'DeviceRGB': return [3 /*break*/, 2];
                            case 'DeviceRGBA': return [3 /*break*/, 3];
                            case 'CMYK': return [3 /*break*/, 4];
                            case 'DeviceCMYK': return [3 /*break*/, 4];
                            case 'Pattern': return [3 /*break*/, 5];
                        }
                        return [3 /*break*/, 6];
                    case 1: return [2 /*return*/, this.gray];
                    case 2: return [2 /*return*/, this.rgb];
                    case 3: return [2 /*return*/, this.rgba];
                    case 4: return [2 /*return*/, this.cmyk];
                    case 5: return [2 /*return*/, new _PdfPatternCS(/* baseCS = */ null)];
                    case 6:
                        if (!(resources instanceof _PdfDictionary)) return [3 /*break*/, 9];
                        colorSpaces = resources.get('ColorSpace');
                        if (!(colorSpaces instanceof _PdfDictionary)) return [3 /*break*/, 9];
                        resourcesCS = colorSpaces.get(cs.name);
                        if (!resourcesCS) return [3 /*break*/, 9];
                        if (!(resourcesCS instanceof _PdfName)) return [3 /*break*/, 8];
                        return [4 /*yield*/, this._parseColorspace(resourcesCS, options)];
                    case 7: return [2 /*return*/, _c.sent()];
                    case 8:
                        cs = resourcesCS;
                        return [3 /*break*/, 10];
                    case 9: return [2 /*return*/, this.gray];
                    case 10:
                        if (!Array.isArray(cs)) return [3 /*break*/, 34];
                        mode = void 0;
                        if (cs[0].name instanceof _PdfReference) {
                            mode = xref._fetch(cs[0]).name;
                        }
                        mode = cs[0].name;
                        params = void 0;
                        numComps = void 0;
                        baseCS = void 0;
                        whitePoint = void 0;
                        blackPoint = void 0;
                        gamma = void 0;
                        matrix = void 0;
                        hiVal = void 0;
                        lookup = void 0;
                        range = void 0;
                        stream = void 0;
                        dictionary = void 0;
                        altRaw = void 0;
                        altCS = void 0;
                        _b = mode;
                        switch (_b) {
                            case 'G': return [3 /*break*/, 11];
                            case 'DeviceGray': return [3 /*break*/, 11];
                            case 'RGB': return [3 /*break*/, 12];
                            case 'DeviceRGB': return [3 /*break*/, 12];
                            case 'CMYK': return [3 /*break*/, 13];
                            case 'DeviceCMYK': return [3 /*break*/, 13];
                            case 'CalGray': return [3 /*break*/, 14];
                            case 'CalRGB': return [3 /*break*/, 15];
                            case 'Pattern': return [3 /*break*/, 16];
                            case 'I': return [3 /*break*/, 19];
                            case 'Indexed': return [3 /*break*/, 19];
                            case 'Separation': return [3 /*break*/, 21];
                            case 'DeviceN': return [3 /*break*/, 21];
                            case 'Lab': return [3 /*break*/, 23];
                            case 'ICCBased': return [3 /*break*/, 24];
                        }
                        return [3 /*break*/, 33];
                    case 11: return [2 /*return*/, this.gray];
                    case 12: return [2 /*return*/, this.rgb];
                    case 13: return [2 /*return*/, this.cmyk];
                    case 14:
                        params = xref._fetch(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        gamma = params.get('Gamma');
                        return [2 /*return*/, new _PdfCalGrayCS(whitePoint, blackPoint, gamma)];
                    case 15:
                        params = xref._fetch(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        gamma = params.getArray('Gamma');
                        matrix = params.getArray('Matrix');
                        return [2 /*return*/, new _PdfColorRgbConverter(whitePoint, blackPoint, gamma, matrix)];
                    case 16:
                        baseCS = cs[1] || null;
                        if (!baseCS) return [3 /*break*/, 18];
                        return [4 /*yield*/, this._subParse(baseCS, options)];
                    case 17:
                        baseCS = _c.sent();
                        _c.label = 18;
                    case 18: return [2 /*return*/, new _PdfPatternCS(baseCS)];
                    case 19: return [4 /*yield*/, this._subParse(cs[1], options)];
                    case 20:
                        baseCS = _c.sent();
                        hiVal = _mathClamp(cs[2] instanceof _PdfReference ? xref._fetch(cs[2]) : cs[2], 0, 255);
                        if (cs[3] instanceof _PdfReference) {
                            lookup = xref._fetch(cs[3]);
                        }
                        else {
                            lookup = cs[3];
                        }
                        return [2 /*return*/, new _PdfIndexedCS(baseCS, hiVal, lookup)];
                    case 21:
                        if (cs[1] instanceof _PdfReference) {
                            name_1 = xref._fetch(cs[1]);
                        }
                        else {
                            name_1 = cs[1];
                        }
                        numComps = Array.isArray(name_1) ? name_1.length : 1;
                        return [4 /*yield*/, this._subParse(cs[2], options)];
                    case 22:
                        baseCS = _c.sent();
                        return [2 /*return*/, new _PdfAlternateCS(numComps, baseCS)];
                    case 23:
                        params = xref._fetch(cs[1]);
                        whitePoint = params.getArray('WhitePoint');
                        blackPoint = params.getArray('BlackPoint');
                        range = params.getArray('Range');
                        return [2 /*return*/, new _PdfLabCS(whitePoint, blackPoint, range)];
                    case 24:
                        stream = xref._fetch(cs[1]);
                        dictionary = stream.dictionary;
                        numComps = dictionary.get('N');
                        _c.label = 25;
                    case 25:
                        _c.trys.push([25, 29, , 30]);
                        colorSpace = new _PdfIccColorSpace('ICCBased', numComps, stream.getBytes());
                        return [4 /*yield*/, colorSpace._initialize(this._canvasRenderCallback.applicationPlatform)];
                    case 26:
                        _c.sent();
                        if (!colorSpace._isUsable) return [3 /*break*/, 28];
                        return [4 /*yield*/, colorSpace._create()];
                    case 27:
                        _c.sent();
                        return [2 /*return*/, colorSpace];
                    case 28: return [3 /*break*/, 30];
                    case 29:
                        ex_1 = _c.sent();
                        altRaw = dictionary.getRaw('Alternate');
                        return [3 /*break*/, 30];
                    case 30:
                        if (!altRaw) return [3 /*break*/, 32];
                        return [4 /*yield*/, this._subParse(altRaw, options)];
                    case 31:
                        altCS = _c.sent();
                        if (altCS.numComps === numComps) {
                            return [2 /*return*/, altCS];
                        }
                        _c.label = 32;
                    case 32:
                        if (numComps === 1) {
                            return [2 /*return*/, this.gray];
                        }
                        else if (numComps === 3) {
                            return [2 /*return*/, this.rgb];
                        }
                        else if (numComps === 4) {
                            return [2 /*return*/, this.cmyk];
                        }
                        return [3 /*break*/, 34];
                    case 33: return [2 /*return*/, this.gray];
                    case 34: return [2 /*return*/, this.gray];
                }
            });
        });
    };
    Object.defineProperty(_PdfColorSpaceUtils.prototype, "gray", {
        get: function () {
            return _defineLazyProperty(this, 'gray', new _PdfDeviceGrayCS());
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(_PdfColorSpaceUtils.prototype, "rgb", {
        get: function () {
            return _defineLazyProperty(this, 'rgb', new _PdfDeviceRgbCS());
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(_PdfColorSpaceUtils.prototype, "rgba", {
        get: function () {
            return _defineLazyProperty(this, 'rgba', new _PdfDeviceRgbaCS());
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(_PdfColorSpaceUtils.prototype, "cmyk", {
        get: function () {
            return _defineLazyProperty(this, 'cmyk', new _PdfDeviceCmykCS());
        },
        enumerable: true,
        configurable: true
    });
    return _PdfColorSpaceUtils;
}());
export { _PdfColorSpaceUtils };
