'use strict';

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

const index = require('./index-fd2bbf0b.js');
const index$1 = require('./index-0568350c.js');
const loader = require('./loader-7137d6eb.js');
const store = require('./store-f5920a57.js');
require('./index-223acf5e.js');
require('./process-es6-39ef8921.js');
require('./global-9027a413.js');
require('./index-81aa1bcb.js');
require('./getCache-c039a6ae.js');
require('./_commonjsHelpers-a5111d61.js');
require('./logic-d8c34975.js');
require('./index-6307644b.js');

const mapCss = "fireenjin-map{position:relative;display:block}fireenjin-map #map{display:block;height:0px;width:100%;margin:0 auto;opacity:0;transition:all ease 0.5s}fireenjin-map.map-is-visible #map{height:var(--map-height, 300px);opacity:1}";

let Map = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fireenjinTrigger = index.createEvent(this, "fireenjinTrigger", 7);
    /**
     * The current list of map markers
     */
    this.mapMarkers = [];
    /**
     * Google Maps options
     */
    this.options = {};
    /**
     * Should the map be visible?
     */
    this.visible = true;
    /**
     * A list of markers to put onto the map
     */
    this.markers = [];
    /**
     * Is the map currently visible?
     */
    this.isVisible = true;
  }
  /**
   * Add a marker to the map
   * @param location The location information for the marker on the map
   */
  async addMarker(location) {
    var _a;
    if (!this.google)
      this.google = ((_a = window === null || window === void 0 ? void 0 : window.google) === null || _a === void 0 ? void 0 : _a.maps)
        ? window.google
        : await this.loadGoogleMaps();
    const marker = typeof location === "string" ? JSON.parse(location) : location;
    const mapMarker = new this.google.maps.Marker({
      position: marker.position,
      map: this.map,
      title: (marker === null || marker === void 0 ? void 0 : marker.name) || "",
      icon: (marker === null || marker === void 0 ? void 0 : marker.icon)
        ? {
          url: marker.icon,
          origin: new this.google.maps.Point(0, 0),
          anchor: new this.google.maps.Point(15, 15),
          scaledSize: new this.google.maps.Size(34, 34),
          shape: { coords: [17, 17, 18], type: "circle" },
          optimized: false,
        }
        : null,
    });
    mapMarker.addListener("click", () => {
      this.onMarkerClick(mapMarker, marker);
    });
    this.mapMarkers.push(mapMarker);
    return mapMarker;
  }
  async updateMarkers() {
    await this.clearMarkers();
    if (this.markers.length >= 1) {
      this.markers.map(this.addMarker.bind(this));
    }
    return this.markers;
  }
  /**
   * Set the list of map markers
   * @param markers A list of map markers
   */
  async setMarkers(markers = []) {
    this.markers = markers;
    await this.clearMarkers();
    if (this.markers.length >= 1) {
      this.markers.map(this.addMarker.bind(this));
    }
    return this.markers;
  }
  async setZoom(level) {
    return this.map.setZoom(level);
  }
  async setCenter(latLng) {
    return this.map.setCenter(latLng);
  }
  /**
   * Clear the markers off of the map
   */
  async clearMarkers() {
    var _a;
    for (let i = 0; i < this.mapMarkers.length; i++) {
      if (!((_a = this.mapMarkers[i]) === null || _a === void 0 ? void 0 : _a.setMap))
        continue;
      this.mapMarkers[i].setMap(null);
    }
    this.mapMarkers = [];
    return true;
  }
  /**
   * When a marker is clicked set the zoom level,
   * animate to center, and emit an event
   * @param marker The map marker that has been clicked
   * @param location The location information tied to the map marker
   */
  onMarkerClick(marker, location) {
    this.map.setZoom(12);
    this.map.setCenter(marker.getPosition());
    this.fireenjinTrigger.emit({
      name: "markerClick",
      payload: {
        marker,
        location,
      },
    });
  }
  /**
   * Get a device's location via Geolocation api and
   * fallback to location based on IP
   * @param callback The function to run when the location is determined
   */
  getLocationCoords(callback) {
    try {
      index$1.Geolocation.getCurrentPosition()
        .then((position) => {
        if (callback && typeof callback === "function") {
          callback(position.coords);
        }
      })
        .catch(async (_error) => {
        fetch("http://ip-api.com/json").then(async (response) => {
          const ISPInfo = await response.json();
          callback({
            latitude: ISPInfo.lat,
            longitude: ISPInfo.lon,
          });
        });
        if (callback && typeof callback === "function") {
          callback(false);
        }
      });
    }
    catch (error) {
      callback(false);
    }
  }
  /**
   * Create the instance of Google Maps
   * @param position The latitude and longitude to center the map on
   */
  async createMap(position) {
    var _a;
    if (!this.google)
      this.google = ((_a = window === null || window === void 0 ? void 0 : window.google) === null || _a === void 0 ? void 0 : _a.maps)
        ? window.google
        : await this.loadGoogleMaps();
    this.map = new this.google.maps.Map(this.mapEl.querySelector("#map"), {
      zoom: 9,
      center: {
        lat: position.latitude,
        lng: position.longitude,
      },
    });
  }
  async loadGoogleMaps(options) {
    const loader$1 = new loader.Loader(this.googleMapsKey, options || {});
    return loader$1.load();
  }
  async componentDidLoad() {
    var _a;
    if (!(index.Build === null || index.Build === void 0 ? void 0 : index.Build.isBrowser))
      return;
    this.isVisible = this.visible;
    this.google = ((_a = window === null || window === void 0 ? void 0 : window.google) === null || _a === void 0 ? void 0 : _a.maps)
      ? window.google
      : await this.loadGoogleMaps();
    setTimeout(() => {
      this.getLocationCoords(async (coords) => {
        this.position = coords
          ? coords
          : { latitude: 38.6270025, longitude: -90.19940419999999 };
        this.createMap(this.position);
        await this.setMarkers(this.markers);
      });
    }, 100);
  }
  render() {
    return (index.h(index.Host, { class: { "map-is-visible": this.isVisible } }, index.h("div", { id: "map" })));
  }
  get mapEl() { return index.getElement(this); }
  static get watchers() { return {
    "markers": ["updateMarkers"]
  }; }
};
Map.style = mapCss;

let ItemJob = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    return (index.h("ion-item", { href: `/jobs/${(_a = this.job) === null || _a === void 0 ? void 0 : _a.id}`, onClick: () => (store.state.job = this.job), detail: true }, index.h("fireenjin-avatar", { slot: "start", style: {
        display: "inline-block",
        position: "relative",
        borderRadius: "4px",
        padding: "0 !important",
        overflow: "hidden",
        marginRight: "10px",
      }, initials: ((_b = this.job) === null || _b === void 0 ? void 0 : _b.title) ? (_c = this.job) === null || _c === void 0 ? void 0 : _c.title.charAt(0) : "", src: (_f = (_e = (_d = this.job) === null || _d === void 0 ? void 0 : _d.photos) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.url }), index.h("ion-label", { class: "ion-text-wrap", style: { position: "relative", textOverflow: "initial" } }, index.h("h2", null, (_g = this.job) === null || _g === void 0 ? void 0 : _g.title), index.h("fireenjin-chip-bar", null, index.h("floodteam-select-status", { jobId: (_h = this.job) === null || _h === void 0 ? void 0 : _h.id, value: (_j = this.job) === null || _j === void 0 ? void 0 : _j.status }), index.h("floodteam-select-location", { disabled: true, jobId: (_k = this.job) === null || _k === void 0 ? void 0 : _k.id, value: (_m = (_l = this.job) === null || _l === void 0 ? void 0 : _l.location) === null || _m === void 0 ? void 0 : _m.id }), index.h("ion-router-link", { onClick: (event) => event.stopPropagation(), href: `/jobs/${(_o = this.job) === null || _o === void 0 ? void 0 : _o.id}/scope` }, index.h("ion-chip", null, index.h("ion-icon", { class: "ft-icon-style", name: "list" }), index.h("ion-label", null, "View Scope")))))));
  }
};

exports.fireenjin_map = Map;
exports.floodteam_item_job = ItemJob;

//# sourceMappingURL=fireenjin-map_2.cjs.entry.js.map