'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-b8c5f2d4.js');
const utils = require('./utils-94ef970a.js');

const DrawController = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.defaultOptions = {
      type: "point",
      multiple: false,
      combine: false,
      delete: false,
      mode: "draw",
    };
  }
  /**
   * Starts a drawing session.
   * @param featureCollection Features with which the session will be
   * initialized.
   * @param options Drawing session options.
   */
  async create(featureCollection, options) {
    let map = utils.getMap(options.mapId);
    if (map == undefined)
      throw "Draw action map not found";
    let toolbar = document.querySelector("gl-draw-toolbar");
    if (toolbar != undefined && options.toolbarLabel != undefined)
      toolbar.label = options.toolbarLabel;
    map.drawOptions = this.getControlOptions(options);
    map.drawing = true;
    if (featureCollection != undefined)
      map.draw.set(featureCollection);
    return await new Promise((resolve) => {
      let cancel = () => {
        map.drawing = false;
        resolve(undefined);
        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(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 != undefined)
      result.styles = opts.styles;
    return result;
  }
};

exports.gl_draw_controller = DrawController;

//# sourceMappingURL=gl-draw-controller.cjs.entry.js.map