import { getMap } from '../utils';
export class DrawController {
    constructor() {
        this.defaultOptions = {
            type: 'point',
            multiple: false,
            combine: false,
            delete: false,
            mode: 'draw'
        };
    }
    async create(featureCollection, options) {
        let map = getMap(options.mapId);
        if (!map)
            throw 'Draw action map not found';
        let toolbar = document.querySelector('gl-draw-toolbar');
        if (toolbar && options.toolbarLabel)
            toolbar.label = options.toolbarLabel;
        map.drawOptions = this.getControlOptions(options);
        map.drawing = true;
        if (featureCollection)
            map.draw.set(featureCollection);
        return await new Promise((resolve) => {
            let cancel = () => {
                map.drawing = false;
                resolve();
                toolbar.removeEventListener('glDrawCancel', cancel);
                toolbar.removeEventListener('glDrawConfirm', confirm);
            };
            let confirm = () => {
                let featureCollection = map.draw.getAll();
                map.drawing = false;
                resolve(featureCollection);
                toolbar.removeEventListener('glDrawConfirm', confirm);
                toolbar.removeEventListener('glDrawConfirm', confirm);
            };
            toolbar.addEventListener('glDrawCancel', cancel);
            toolbar.addEventListener('glDrawConfirm', confirm);
        });
    }
    getControlOptions(options) {
        let opts = Object.assign({}, this.defaultOptions, (options || {}));
        let mode = 'simple_select';
        if (opts.mode === 'direct')
            mode = 'direct_select';
        if (opts.mode === 'draw') {
            if (opts.type === 'point')
                mode = 'draw_point';
            if (opts.type === 'line')
                mode = 'draw_line_string';
            if (opts.type === 'polygon')
                mode = 'draw_polygon';
        }
        let result = {
            controls: {
                point: opts.multiple && opts.type === 'point',
                line_string: opts.multiple && opts.type === 'line',
                polygon: opts.multiple && opts.type === 'polygon',
                trash: opts.delete,
                combine_features: opts.multiple && opts.combine,
                uncombine_features: opts.multiple && opts.combine
            },
            defaultMode: mode
        };
        if (opts.styles)
            result.styles = opts.styles;
        return result;
    }
    static get is() { return "gl-draw-controller"; }
    static get methods() { return {
        "create": {
            "complexType": {
                "signature": "(featureCollection: any, options: DrawOptions) => Promise<unknown>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }, {
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "DrawOptions": {
                        "location": "import",
                        "path": "./interface"
                    }
                },
                "return": "Promise<unknown>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
}
