/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
import { Palette } from './modules/palette.js';
export const Utils = {
    /*
    * Begin a vector shape path on current context
    *
    * @return void
    */
    startShape(session) {
        session.ctx.beginPath();
    },
    /*
    * End a vector shape path on current context, close and bring back to origin
    *
    * if specified
    * @param {Boolean} close
    * @param {Boolean} back
    * @return void
    */
    endShape(session, close, back) {
        close = typeof close === 'undefined' ? true : close;
        if (back) {
            session.ctx.lineTo(session.pos.x * session.ratio, session.pos.y * session.ratio);
        }
        if (close) {
            session.ctx.closePath();
        }
        if (session.settings.stroke.width) {
            session.ctx.stroke();
        }
        session.ctx.fillStyle = session.settings.fill;
        session.ctx.fill();
    },
    /*
    * Parse stroke attributes from string and return stroke settings object
    *
    * @param {Boolean} close
    * @return {Object}
    */
    parseLineStyle(attributes) {
        var out = {}, i, attr;
        for (i = 0; i < attributes.length; i += 1) {
            attr = attributes[i];
            if (typeof attr === 'number') {
                out.width = attr;
            }
            else {
                out.color = attr;
            }
        }
        return out;
    },
    isString(val) {
        return typeof val === 'string';
    },
    isNumber(val) {
        return typeof val === 'number';
    },
    /*
    * Parse or evaluate a coordinate from string or number
    *
    * @param {String|Number} val
    * @param {String} type
    * @return {Number}
    */
    parseCoordinate(session, val, type) {
        type = type || 'x';
        if (Utils.isString(val)) {
            if (type === 'x') {
                switch (val) {
                    case 'center':
                        return Utils.getCenter(session).x;
                    case 'right':
                        return session.width;
                    case 'left':
                        return 0;
                }
            }
            else if (type === 'y') {
                switch (val) {
                    case 'center':
                        return Utils.getCenter(session).y;
                    case 'bottom':
                        return session.height;
                    case 'top':
                        return 0;
                }
            }
        }
        else if (Utils.isNumber(val)) {
            return val;
        }
        return 0;
    },
    /*
    * Parse a pair of x / y coordinates
    *
    * @param {String|Number} x
    * @param {String|Number} y
    * @return {Object}
    */
    parseCoordinates(session, x, y) {
        const newX = Utils.parseCoordinate(session, x, 'x');
        const newY = Utils.parseCoordinate(session, y, 'y');
        return { x: newX, y: newY };
    },
    /*
    * Get stage center in current session
    *
    * @return {Object}
    */
    getCenter(session) {
        return {
            x: session.width / 2,
            y: session.height / 2
        };
    },
    /*
    * Get a color from palette if it's an existing key or just return
    *
    * @param {String} val
    * @return void
    */
    parseColor(val) {
        return Palette[val] || val;
    },
    /*
    * Given value is a valid color
    *
    * @param {*} val
    * @return {Boolean}
    */
    isColorValue(val) {
        if (typeof val !== 'string') {
            return false;
        }
        if (val.substr(0, 1) === '#' && val.length > 3 && val.length <= 7) {
            return true;
        }
        else if (Palette[val]) {
            return true;
        }
        return false;
    },
};
//# sourceMappingURL=utils.js.map