/*
 * Copyright (c) 2018 by Filestack
 * Some rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { config } from './../config';
import { TransformSchema } from './../schema/transforms.schema';
import { getValidator } from './../schema/validator';
import { resolveHost } from './utils';
import { FilestackError } from './../FilestackError';
/**
 * Align enum
 */
export var AlignOptions;
(function (AlignOptions) {
    AlignOptions["left"] = "left";
    AlignOptions["right"] = "right";
    AlignOptions["center"] = "center";
    AlignOptions["bottom"] = "bottom";
    AlignOptions["top"] = "top";
})(AlignOptions || (AlignOptions = {}));
/**
 * Align enum with faces option
 */
export var AlignFacesOptions;
(function (AlignFacesOptions) {
    AlignFacesOptions["left"] = "left";
    AlignFacesOptions["right"] = "right";
    AlignFacesOptions["center"] = "center";
    AlignFacesOptions["bottom"] = "bottom";
    AlignFacesOptions["top"] = "top";
    AlignFacesOptions["faces"] = "faces";
})(AlignFacesOptions || (AlignFacesOptions = {}));
/**
 * Fit enum
 */
export var FitOptions;
(function (FitOptions) {
    FitOptions["clip"] = "clip";
    FitOptions["crop"] = "crop";
    FitOptions["scale"] = "scale";
    FitOptions["max"] = "max";
})(FitOptions || (FitOptions = {}));
/**
 * Blur enum
 */
export var BlurMode;
(function (BlurMode) {
    BlurMode["linear"] = "linear";
    BlurMode["gaussian"] = "gaussian";
})(BlurMode || (BlurMode = {}));
/**
 * Shapes enum
 */
export var ShapeType;
(function (ShapeType) {
    ShapeType["oval"] = "oval";
    ShapeType["rect"] = "rect";
})(ShapeType || (ShapeType = {}));
/**
 * Noise type enum
 */
export var NoiseType;
(function (NoiseType) {
    NoiseType["none"] = "none";
    NoiseType["low"] = "low";
    NoiseType["medium"] = "medium";
    NoiseType["high"] = "high";
})(NoiseType || (NoiseType = {}));
/**
 * Style type enum
 */
export var StyleType;
(function (StyleType) {
    StyleType["artwork"] = "artwork";
    StyleType["photo"] = "photo";
})(StyleType || (StyleType = {}));
/**
 * Color space enum
 */
export var ColorspaceType;
(function (ColorspaceType) {
    ColorspaceType["RGB"] = "RGB";
    ColorspaceType["CMYK"] = "CMYK";
    ColorspaceType["Input"] = "Input";
})(ColorspaceType || (ColorspaceType = {}));
/**
 * Crop faces options enum
 */
export var CropfacesType;
(function (CropfacesType) {
    CropfacesType["thumb"] = "thumb";
    CropfacesType["crop"] = "crop";
    CropfacesType["fill"] = "fill";
})(CropfacesType || (CropfacesType = {}));
/**
 * Convert to format
 */
export var VideoTypes;
(function (VideoTypes) {
    VideoTypes["h264"] = "h264";
    VideoTypes["h264_hi"] = "h264.hi";
    VideoTypes["webm"] = "webm";
    VideoTypes["webm-hi"] = "webm.hi";
    VideoTypes["ogg"] = "ogg";
    VideoTypes["ogg-hi"] = "ogg.hi";
    VideoTypes["hls-variant"] = "hls.variant";
    VideoTypes["mp3"] = "mp3";
    VideoTypes["oga"] = "oga";
    VideoTypes["m4a"] = "m4a";
    VideoTypes["aac"] = "aac";
    VideoTypes["hls"] = "hls.variant.audio";
})(VideoTypes || (VideoTypes = {}));
export var URLScreenshotAgent;
(function (URLScreenshotAgent) {
    URLScreenshotAgent["desktop"] = "desktop";
    URLScreenshotAgent["mobile"] = "mobile";
})(URLScreenshotAgent || (URLScreenshotAgent = {}));
export var URLScreenshotMode;
(function (URLScreenshotMode) {
    URLScreenshotMode["all"] = "all";
    URLScreenshotMode["window"] = "window";
})(URLScreenshotMode || (URLScreenshotMode = {}));
export var URLScreenshotOrientation;
(function (URLScreenshotOrientation) {
    URLScreenshotOrientation["portrait"] = "portrait";
    URLScreenshotOrientation["landscape"] = "landscape";
})(URLScreenshotOrientation || (URLScreenshotOrientation = {}));
/**
 * Video storage location
 */
export var Locations;
(function (Locations) {
    Locations["s3"] = "s3";
    Locations["azure"] = "azure";
    Locations["gcs"] = "gcs";
    Locations["rackspace"] = "rackspace";
    Locations["dropbox"] = "dropbox";
})(Locations || (Locations = {}));
export var VideoAccess;
(function (VideoAccess) {
    VideoAccess["private"] = "private";
    VideoAccess["public"] = "public";
})(VideoAccess || (VideoAccess = {}));
export var VideoAccessMode;
(function (VideoAccessMode) {
    VideoAccessMode["preserve"] = "preserve";
    VideoAccessMode["constrain"] = "constrain";
    VideoAccessMode["letterbox"] = "letterbox";
    VideoAccessMode["pad"] = "pad";
    VideoAccessMode["crop"] = "crop";
})(VideoAccessMode || (VideoAccessMode = {}));
var handleRegexp = /^[\w\-]{20}$/;
/**
 * Class for handling filelinks. For now its supports all filestack transforms.
 * It outputs transform url or array of transforms
 * @example
 * const link = new Filelink('handle or externalUrl', 'apikey');
 * link.flip().flop().store();
 *
 * console.log(link.toString());
 * // enable base64 support
 * link.setBase64(true)
 *
 * console.log(link.toString());
 *
 * @export
 * @class Filelink
 */
var Filelink = /** @class */ (function () {
    /**
     * Class for generating tranformation urls
     * @param {(string | string[])} source - handle or multiple handles (i.e. for collage)
     * @param {string} [apikey] - your apikey - required for all external sources
     *
     * @memberof Filelink
     */
    function Filelink(source, apikey) {
        /**
         * Validator instance
         *
         * @private
         * @memberof Filelink
         */
        this.validator = getValidator(TransformSchema);
        /**
         * Applied transforms array
         *
         * @private
         * @memberof Filelink
         */
        this.transforms = [];
        /**
         * Is base64 support is enabled
         *
         * @private
         * @type {boolean}
         * @memberof Filelink
         */
        this.b64 = false;
        this.source = source;
        var isExternal = this.isSourceExternal();
        if (isExternal && !apikey) {
            throw new FilestackError('External sources requires apikey to handle transforms');
        }
        if (!isExternal && typeof this.source === 'string' && !handleRegexp.test(this.source)) {
            throw new FilestackError('Invalid filestack source provided');
        }
        this.apikey = apikey;
    }
    /**
     * Enable new base64 link support to avoid problems with special chars in link
     *
     * @param {boolean} flag
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.setBase64 = function (flag) {
        this.b64 = flag;
        return this;
    };
    /**
     * Set cname for transformation link
     *
     * @param {string} cname
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.setCname = function (cname) {
        this.cname = cname;
        return this;
    };
    /**
     * Set custom domain. Used for test purpose. It will be removed when after client.transform
     *
     * @param {string} domain
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.setCustomDomain = function (domain) {
        this.customDomain = domain;
        return this;
    };
    /**
     * Returns JSONSchema form transformations params
     *
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.getValidationSchema = function () {
        return TransformSchema;
    };
    /**
     * Returns transformations in JSON format
     *
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.getTransformations = function () {
        return this.transforms;
    };
    /**
     * Returns transform url
     *
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.toString = function () {
        var returnUrl = [];
        returnUrl.push(this.getCdnHost());
        if (this.apikey) {
            returnUrl.push(this.apikey);
        }
        var transformsString = this.generateTransformString();
        var source = this.source;
        if (this.b64) {
            if (this.transforms.length > 0) {
                transformsString = "b64/" + btoa(JSON.stringify(this.transforms));
            }
            if (Array.isArray(source)) {
                source = this.arrayToString(source);
            }
            source = "b64://" + btoa(source);
        }
        else {
            if (Array.isArray(source)) {
                source = this.arrayToString(source);
            }
            else {
                source = this.escapeValue(source);
            }
        }
        if (transformsString.length) {
            returnUrl.push(transformsString);
        }
        returnUrl.push(source);
        return returnUrl.join('/');
    };
    /**
     * Add task and validate
     *
     * @param {string} name
     * @param {*} [params]
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.addTask = function (name, params) {
        this.validateTask(name, params);
        if (name !== 'cache' && typeof params === 'boolean') {
            if (!params) {
                return this;
            }
            else {
                params = undefined;
            }
        }
        else if (typeof params === 'object' && !Object.keys(params).length) {
            params = undefined;
        }
        this.transforms.push({ name: name, params: params });
        return this;
    };
    /**
     * Cleanup transformations on filelink
     *
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.reset = function () {
        this.transforms = [];
        return this;
    };
    /**
     * Transformations part
     */
    /**
     * Adds flip transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#flip
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.flip = function () {
        return this.addTask('flip', true);
    };
    /**
     * Adds flop transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#flop
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.flop = function () {
        return this.addTask('flop', true);
    };
    /**
     * Adds enhance transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#enhance
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.enhance = function () {
        return this.addTask('enhance', true);
    };
    /**
     * Add security to link
     *
     * @see https://www.filestack.com/docs/api/processing/#redeye
     * @returns
     * @memberof Filelink
     */
    Filelink.prototype.redeye = function () {
        return this.addTask('redeye', true);
    };
    /**
     * Add monochrome transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#monochrome
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.monochrome = function () {
        return this.addTask('monochrome', true);
    };
    /**
     * Adds negative transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#negative
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.negative = function () {
        return this.addTask('negative', true);
    };
    /**
     * Adds enchance transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#tags
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.tags = function () {
        return this.addTask('tags', true);
    };
    /**
     * Adds sfw transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#sfw
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.sfw = function () {
        return this.addTask('sfw', true);
    };
    /**
     * Adds store transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#store
     * @param {(StoreParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.store = function (params) {
        return this.addTask('store', params);
    };
    /**
     * Adds cache transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#cache
     * @param {(CacheParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.cache = function (params) {
        return this.addTask('cache', params);
    };
    /**
     * Adds resize transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#resize
     * @param {ResizeParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.resize = function (params) {
        return this.addTask('resize', params);
    };
    /**
     * Adds crop transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#crop
     * @param {CropParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.crop = function (params) {
        return this.addTask('crop', params);
    };
    /**
     * Adds rotate transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#rotate
     * @param {RotateParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.rotate = function (params) {
        return this.addTask('rotate', params);
    };
    /**
     * Adds detect_faces transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#facial-detection
     * @param {(DetectFacesParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.detectFaces = function (params) {
        return this.addTask('detect_faces', params);
    };
    /**
     * Adds crop faces transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#crop-faces
     * @param {CropFacesParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.cropFaces = function (params) {
        return this.addTask('crop_faces', params);
    };
    /**
     * Adds pixelate faces transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#pixelate-faces
     * @param {PixelateFacesParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.pixelateFaces = function (params) {
        return this.addTask('pixelate_faces', params);
    };
    /**
     * Adds blur faces transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#blur-faces
     * @param {BlurFacesParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.blurFaces = function (params) {
        return this.addTask('blur_faces', params);
    };
    /**
     * Adds rounded corners transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#rounded-corners
     * @param {(RoundedCornersParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.roundedCorners = function (params) {
        return this.addTask('rounded_corners', params);
    };
    /**
     * Adds polaroid transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#polaroid
     * @param {(PolaroidParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.polaroid = function (params) {
        return this.addTask('polaroid', params);
    };
    /**
     * Adds vignette transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#vignette
     * @param {(VignetteParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.vignette = function (params) {
        return this.addTask('vignette', params);
    };
    /**
     * Adds torn edges transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#torn-edges
     * @param {(TornEdgesParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.tornEdges = function (params) {
        return this.addTask('torn_edges', params);
    };
    /**
     * Adds shadow transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#shadow
     * @param {(ShadowParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.shadow = function (params) {
        return this.addTask('shadow', params);
    };
    /**
     * Adds circle transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#circle
     * @param {(CircleParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.circle = function (params) {
        return this.addTask('circle', params);
    };
    /**
     * Adds border transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#border
     * @param {(BorderParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.border = function (params) {
        return this.addTask('border', params);
    };
    /**
     * Adds sharpen transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#sharpen
     * @param {(SharpenParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.sharpen = function (params) {
        return this.addTask('sharpen', params);
    };
    /**
     * Adds blur transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#blur
     * @param {(BlurParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.blur = function (params) {
        return this.addTask('blur', params);
    };
    /**
     * Adds blackwhite transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#blackwhite
     * @param {(BlackwhiteParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.blackwhite = function (params) {
        return this.addTask('blackwhite', params);
    };
    /**
     * Adds sepia transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#sepia
     * @param {(SepiaParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.sepia = function (params) {
        return this.addTask('sepia', params);
    };
    /**
     * Adds pixelate transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#pixelate
     * @param {(PixelateParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.pixelate = function (params) {
        return this.addTask('pixelate', params);
    };
    /**
     * Adds oilpaint transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#oil-paint
     * @param {(OilPaintParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.oilPaint = function (params) {
        return this.addTask('oil_paint', params);
    };
    /**
     * Adds modulate transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#modulate
     * @param {(ModulateParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.modulate = function (params) {
        return this.addTask('modulate', params);
    };
    /**
     * Adds partial pixelate transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#partial-pixelate
     * @param {PartialPixelateParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.partialPixelate = function (params) {
        return this.addTask('partial_pixelate', params);
    };
    /**
     * Adds partial blur transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#partial-blur
     * @param {PartialBlurParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.partialBlur = function (params) {
        return this.addTask('partial_blur', params);
    };
    /**
     * Adds collage transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#collage
     * @param {CollageParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.collage = function (params) {
        return this.addTask('collage', params);
    };
    /**
     * Adds upscale transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#upscale
     * @param {(UpscaleParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.upscale = function (params) {
        return this.addTask('upscale', params);
    };
    /**
     * Adds ascii transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#ascii
     * @param {(AsciiParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.ascii = function (params) {
        return this.addTask('ascii', params);
    };
    /**
     * Adds quality transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#quality
     * @param {QualityParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.quality = function (params) {
        return this.addTask('quality', params);
    };
    /**
     * Adds security transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#security
     * @param {SecurityParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.security = function (params) {
        return this.addTask('security', params);
    };
    /**
     * Adds output transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#output
     * @param {OutputParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.output = function (params) {
        return this.addTask('output', params);
    };
    /**
     * Adds video convert transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#video-convert
     * @param {VideoConvertParams} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.videoConvert = function (params) {
        return this.addTask('video_convert', params);
    };
    /**
     * Adds URLScreenshot transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#urlscreenshot
     * @param {(URLScreenshotParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.URLScreenshot = function (params) {
        return this.addTask('urlscreenshot', params);
    };
    /**
     * Adds pdfinfo transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#pdfinfo
     * @param {(PdfInfoParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.PDFInfo = function (params) {
        return this.addTask('pdfinfo', params);
    };
    /**
     * Adds pdfconvert transformation
     *
     * @see https://www.filestack.com/docs/api/processing/#pdfconvert
     * @param {(PdfConvertParams | boolean)} params
     * @returns this
     * @memberof Filelink
     */
    Filelink.prototype.PDFConvert = function (params) {
        return this.addTask('pdfconvert', params);
    };
    /**
     * Checks if source is external
     *
     * @private
     * @returns {boolean}
     * @memberof Filelink
     */
    Filelink.prototype.isSourceExternal = function () {
        if (!this.source) {
            throw new FilestackError('Source not Set');
        }
        var toTest = Array.isArray(this.source) ? this.source : [this.source];
        for (var i in toTest) {
            if (!toTest.hasOwnProperty(i)) {
                continue;
            }
            if (toTest[i].indexOf('src:') === 0 || toTest[i].indexOf('http') === 0) {
                return true;
            }
        }
        return false;
    };
    /**
     * Validate single task against schema
     *
     * @private
     * @param {*} name
     * @param {*} options
     * @returns {void}
     * @memberof Filelink
     */
    Filelink.prototype.validateTask = function (name, options) {
        var toValidate = {};
        toValidate[name] = options;
        var res = this.validator(toValidate);
        if (res.errors.length) {
            throw new FilestackError("Task \"" + name + "\" validation error, Params: " + JSON.stringify(options), res.errors);
        }
        return;
    };
    /**
     * Returns correct cdn url with cname support
     *
     * @private
     * @returns {string}
     * @memberof Filelink
     */
    Filelink.prototype.getCdnHost = function () {
        var urls = config.urls;
        if (this.customDomain) {
            urls.cdnUrl = this.customDomain;
        }
        urls = resolveHost(urls, this.cname);
        return urls.cdnUrl;
    };
    /**
     * Returns applied transformations as string
     *
     * @private
     * @returns {string}
     * @memberof Filelink
     */
    Filelink.prototype.generateTransformString = function () {
        var _this = this;
        var transforms = [];
        this.transforms.forEach(function (el) {
            transforms.push(_this.optionToString(el.name, el.params));
        });
        return transforms.join('/');
    };
    /**
     * Flatten transformation option to string
     *
     * @private
     * @example {resize:{width: 100,height: 200}} => resize=width:100,height:200
     * @param key - option key
     * @param values - option params
     */
    Filelink.prototype.optionToString = function (key, values) {
        var _this = this;
        var optionsString = [];
        if (typeof values === 'undefined') {
            return key;
        }
        if (typeof values === 'object' && !Object.keys(values).length) {
            return key;
        }
        // if we just want to enable feature
        if (typeof values === 'boolean') {
            if (!values) {
                if (key === 'cache') {
                    return 'cache=false';
                }
                return '';
            }
            return key;
        }
        Object.keys(values).forEach(function (i) {
            if (Array.isArray(values[i])) {
                optionsString.push(i + ":" + _this.arrayToString(values[i]));
                return;
            }
            optionsString.push(i + ":" + _this.escapeValue(values[i]));
        });
        return key + "=" + optionsString.join(',');
    };
    /**
     * Escape params values
     *
     * @private
     * @param {string} value
     * @returns {string}
     * @memberof Filelink
     */
    Filelink.prototype.escapeValue = function (value) {
        if (typeof value !== 'string') {
            return value;
        }
        if (value.indexOf('/') > -1) {
            return "\"" + value + "\"";
        }
        return value;
    };
    /**
     * Converts nested arrays to string
     *
     * @private
     * @example [1,2, [2,3]] => "[1,2, [2,3]]"
     * @param arr - any array
     */
    Filelink.prototype.arrayToString = function (arr) {
        var _this = this;
        var toReturn = arr.map(function (el) {
            if (Array.isArray(el)) {
                return _this.arrayToString(el);
            }
            return _this.escapeValue(el);
        });
        return "[" + toReturn + "]";
    };
    return Filelink;
}());
export { Filelink };
