# All files index.js

90.8% Statements 316/348
84.01% Branches 205/244
96% Functions 48/50
90.88% Lines 299/329

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912

    1x

    1x
    1x

    9x
    1x
    1x
    1x

    9x

    1x
    1x

    8x
    8x
    3x

    5x
    9x
    9x

    5x
    9x
    9x
    9x

    5x

    5x
    4x
    4x
    4x
    4x

    5x
    5x

    1x

    79x
    19x

    79x

    35x
    35x
    35x

    38x

    38x
    1x

    38x
    2x

    36x
    1x

    32x
    32x

    31x

    31x

    35x
    18x
    18x
    18x

    18x

    18x
    2x
    2x

    16x

    18x
    15x

    3x

    18x
    3x

    15x
    15x

    15x

    1x

    17x

    1x

    15x

    15x

    16x
    16x
    16x

    16x

    16x

    16x
    16x

    16x
    14x

    16x
    2x

    2x
    1x

    16x

    16x

    15x
    14x
    14x

    14x
    1x
    1x

    2x
    2x
    2x
    2x

    108x
    4044x
    108x
    108x

    108x

    24x
    24x
    10x
    10x
    10x

    14x
    14x
    14x
    14x
    6x

    6x

    14x
    14x
    15x
    15x
    13x
    13x

    13x
    10x
    22x

    13x
    13x
    13x
    13x
    13x
    13x
    5x

    5x

    13x
    13x
    2x
    1x
    1x
    1x

    14x
    11x

    14x

    14x
    14x

    14x

    11x
    11x

    11x
    17x
    17x
    11x
    11x
    11x
    11x
    11x
    11x
    11x

    11x

    11x
    6x

    11x

    11x

    1x

    9x

    9x
    1x

    8x

    9x

    11x
    11x

    11x

    29x
    29x
    29x

    28x

    28x

    29x
    10x

    29x
    10x

    29x
    19x

    29x
    8x

    8x
    8x

    29x
    29x

    29x
    1020x
    1020x
    1020x

    1020x
    13x

    1007x

    1007x
    34x
    5x
    5x

    34x
    34x
    34x
    16x
    16x
    4x
    4x
    16x

    12x
    6x

    34x
    27x

    34x
    14x
    20x
    11x
    11x

    11x
    11x

    11x
    9x
    9x

    34x
    34x
    34x

    1007x

    29x
    29x
    29x

    29x
    29x
    29x
    29x

    29x
    24x

    29x
    9x
    9x

    9x
    9x

    9x

    9x

    9x
    9x
    9x
    9x

    9x

    9x

    20x
    20x

    19x

    29x

    3x
    2x

    3x
    3x

    3x

    39x
    39x
    39x
    39x
    1511x
    1511x
    1010x
    1010x

    39x
    39x
    38x
    38x
    1x

    1x

    37x
    25x
    25x
    24x
    24x
    6x

    24x
    5x

    37x

    25x

    24x
    24x

    1x

    12x

    39x
    39x
    37x

    39x
    18x

    21x

    1x
    1x
    1x
    1x
    1x

    2x
    2x
    2x
    4x
    1x

    2x

    2x
    2x
    3x
    3x
    1x

     /*
    ol-mapbox-style - Use Mapbox Style objects with OpenLayers
    Copyright 2016-present ol-mapbox-style contributors
    License: https://raw.githubusercontent.com/openlayers/ol-mapbox-style/master/LICENSE
    */

    import GeoJSON from 'ol/format/GeoJSON.js';
    import MVT from 'ol/format/MVT.js';
    import Map from 'ol/Map.js';
    import TileGrid from 'ol/tilegrid/TileGrid.js';
    import TileJSON from 'ol/source/TileJSON.js';
    import TileLayer from 'ol/layer/Tile.js';
    import VectorLayer from 'ol/layer/Vector.js';
    import VectorSource from 'ol/source/Vector.js';
    import VectorTileLayer from 'ol/layer/VectorTile.js';
    import VectorTileSource from 'ol/source/VectorTile.js';
    import View from 'ol/View.js';
    import applyStyleFunction, {
      _colorWithOpacity,
      getValue,
    } from './stylefunction.js';
    import googleFonts from 'webfont-matcher/lib/fonts/google.js';
    import mb2css from 'mapbox-to-css-font';
    import {Color} from '@mapbox/mapbox-gl-style-spec';
    import {assign, defaultResolutions} from './util.js';
    import {createXYZ} from 'ol/tilegrid.js';
    import {fromLonLat} from 'ol/proj.js';
    import {isEmpty} from 'ol/obj.js';
    import {unByKey} from 'ol/Observable.js';

    /**
     * @typedef {import("ol/Map").default} PluggableMap
     * @typedef {import("ol/layer/Layer").default} Layer
     * @typedef {import("ol/source/Source").default} Source
     * @private
     */

    const tilejsonCache = {};

    const fontFamilyRegEx = /font-family: ?([^;]*);/;
    const stripQuotesRegEx = /("|')/g;
    let loadedFontFamilies;
    function hasFontFamily(family) {
      if (!loadedFontFamilies) {
        loadedFontFamilies = {};
        const styleSheets = document.styleSheets;
        for (let i = 0, ii = styleSheets.length; i < ii; ++i) {
          const styleSheet = /** @type {CSSStyleSheet} */ (styleSheets[i]);
          try {
            const cssRules = styleSheet.rules || styleSheet.cssRules;
            if (cssRules) {
              for (let j = 0, jj = cssRules.length; j < jj; ++j) {
                const cssRule = cssRules[j];
                if (cssRule.type == 5) {
                  const match = cssRule.cssText.match(fontFamilyRegEx);
                  loadedFontFamilies[match[1].replace(stripQuotesRegEx, '')] = true;
                }
              }
            }
          } catch (e) {
            // empty catch block
          }
        }
      }
      return family in loadedFontFamilies;
    }

    const processedFontFamilies = {};
    const googleFamilies = googleFonts.getNames();

    /**
     * @private
     * @param {Array} fonts Fonts.
     * @return {Array} Processed fonts.
     */
    function getFonts(fonts) {
      const fontsKey = fonts.toString();
      if (fontsKey in processedFontFamilies) {
        return fonts;
      }
      const googleFontDescriptions = fonts.map(function (font) {
        const parts = mb2css(font, 1).split(' ');
        return [parts.slice(3).join(' ').replace(/"/g, ''), parts[1] + parts[0]];
      });
      for (let i = 0, ii = googleFontDescriptions.length; i < ii; ++i) {
        const googleFontDescription = googleFontDescriptions[i];
        const family = googleFontDescription[0];
        if (!hasFontFamily(family) && googleFamilies.indexOf(family) !== -1) {
          const fontUrl =
            'https://fonts.googleapis.com/css?family=' +
            family.replace(/ /g, '+') +
            ':' +
            googleFontDescription[1];
          if (!document.querySelector('link[href="' + fontUrl + '"]')) {
            const markup = document.createElement('link');
            markup.href = fontUrl;
            markup.rel = 'stylesheet';
            document.head.appendChild(markup);
          }
        }
      }
      processedFontFamilies[fontsKey] = true;
      return fonts;
    }

    const spriteRegEx = /^(.*)(\?.*)$/;

    function withPath(url, path) {
      if (path && url.indexOf('.') === 0) {
        url = path + url;
      }
      return url;
    }

    function toSpriteUrl(url, path, extension) {
      url = withPath(url, path);
      const parts = url.match(spriteRegEx);
      return parts
        ? parts[1] + extension + (parts.length > 2 ? parts[2] : '')
        : url + extension;
    }

    /**
     * ```js
     * import {applyStyle} from 'ol-mapbox-style';
     * ```
     *
     * Applies a style function to an `ol.layer.VectorTile` or `ol.layer.Vector`
     * with an `ol.source.VectorTile` or an `ol.source.Vector`. The style function
     * will render all layers from the `glStyle` object that use the specified
     * `source`, or a subset of layers from the same source. The source needs to be
     * a `"type": "vector"` or `"type": "geojson"` source.
     *
     * Two additional properties will be set on the provided layer:
     *
     *  * `mapbox-source`: The `id` of the Mapbox Style document's source that the
     *    OpenLayers layer was created from. Usually `apply()` creates one
     *    OpenLayers layer per Mapbox Style source, unless the layer stack has
     *    layers from different sources in between.
     *  * `mapbox-layers`: The `id`s of the Mapbox Style document's layers that are
     *    included in the OpenLayers layer.
     *
     * @param {VectorTileLayer|VectorLayer} layer OpenLayers layer.
     * @param {string|Object} glStyle Mapbox Style object.
     * @param {string|Array<string>} source `source` key or an array of layer `id`s from the
     * Mapbox Style object. When a `source` key is provided, all layers for the
     * specified source will be included in the style function. When layer `id`s
     * are provided, they must be from layers that use the same source.
     * @param {string} [path=undefined] Path of the style file. Only required when
     * a relative path is used with the `"sprite"` property of the style.
     * @param {Array<number>} [resolutions=undefined] Resolutions for mapping resolution to zoom level.
     * @return {Promise} Promise which will be resolved when the style can be used
     * for rendering.
     */
    export function applyStyle(layer, glStyle, source, path, resolutions) {
      return new Promise(function (resolve, reject) {
        // TODO: figure out where best place to check source type is
        // Note that the source arg is an array of gl layer ids and each must be
        // dereferenced to get source type to validate
        if (typeof glStyle != 'object') {
          glStyle = JSON.parse(glStyle);
        }
        if (glStyle.version != 8) {
          return reject(new Error('glStyle version 8 required.'));
        }
        if (!(layer instanceof VectorLayer || layer instanceof VectorTileLayer)) {
          return reject(
            new Error('Can only apply to VectorLayer or VectorTileLayer')
          );
        }

        let spriteScale, spriteData, spriteImageUrl, style;
        function onChange() {
          Eif (!style && (!glStyle.sprite || spriteData)) {
            style = applyStyleFunction(
              layer,
              glStyle,
              source,
              resolutions,
              spriteData,
              spriteImageUrl,
              getFonts
            );
            Iif (!layer.getStyle()) {
              reject(new Error(`Nothing to show for source [${source}]`));
            } else {
              resolve();
            }
          } else if (style) {
            layer.setStyle(style);
            resolve();
          } else {
            reject(new Error('Something went wrong trying to apply style.'));
          }
        }

        if (glStyle.sprite) {
          spriteScale = window.devicePixelRatio >= 1.5 ? 0.5 : 1;
          const sizeFactor = spriteScale == 0.5 ? '@2x' : '';
          let spriteUrl = toSpriteUrl(glStyle.sprite, path, sizeFactor + '.json');

          fetch(spriteUrl, {credentials: 'same-origin'})
            .then(function (response) {
              if (!response.ok && sizeFactor !== '') {
                spriteUrl = toSpriteUrl(glStyle.sprite, path, '.json');
                return fetch(spriteUrl, {credentials: 'same-origin'});
              } else {
                return response;
              }
            })
            .then(function (response) {
              if (response.ok) {
                return response.json();
              } else {
                reject(
                  new Error(
                    `Problem fetching sprite from ${spriteUrl}: ${response.statusText}`
                  )
                );
              }
            })
            .then(function (spritesJson) {
              if (spritesJson === undefined) {
                return reject(new Error('No sprites found.'));
              }
              spriteData = spritesJson;
              spriteImageUrl = toSpriteUrl(
                glStyle.sprite,
                path,
                sizeFactor + '.png'
              );
              onChange();
            })
            .catch(function (err) {
              reject(
                new Error(`Sprites cannot be loaded: ${spriteUrl}: ${err.message}`)
              );
            });
        } else {
          onChange();
        }
      });
    }

    const emptyObj = {};

    function setBackground(mapOrLayer, layer) {
      const background = {
        id: layer.id,
        type: layer.type,
      };
      const functionCache = {};
      function updateStyle(resolution) {
        const layout = layer.layout || {};
        const paint = layer.paint || {};
        background['paint'] = paint;
        const zoom =
          typeof mapOrLayer.getSource === 'function'
            ? mapOrLayer.getSource().getTileGrid().getZForResolution(resolution)
            : mapOrLayer.getView().getZoom();
        const element =
          typeof mapOrLayer.getTargetElement === 'function'
            ? mapOrLayer.getTargetElement()
            : undefined;
        let bg, opacity;
        Eif (paint['background-color'] !== undefined) {
          bg = getValue(
            background,
            'paint',
            'background-color',
            zoom,
            emptyObj,
            functionCache
          );
          if (element) {
            element.style.background = Color.parse(bg).toString();
          }
        }
        if (paint['background-opacity'] !== undefined) {
          opacity = getValue(
            background,
            'paint',
            'background-opacity',
            zoom,
            emptyObj,
            functionCache
          );
          if (element) {
            element.style.opacity = opacity;
          }
        }
        Iif (layout.visibility == 'none') {
          if (element) {
            element.style.backgroundColor = '';
            element.style.opacity = '';
            return undefined;
          }
        }
        return _colorWithOpacity(bg, opacity);
      }
      if (typeof mapOrLayer.getTargetElement === 'function') {
        Eif (mapOrLayer.getTargetElement()) {
          updateStyle();
        }
        mapOrLayer.on(['change:resolution', 'change:target'], updateStyle);
      } else Eif (typeof mapOrLayer.setBackground === 'function') {
        mapOrLayer.setBackground(updateStyle);
      } else {
        throw new Error('Unable to apply background.');
      }
    }

    /**
     * ```js
     * import {applyBackground} from 'ol-mapbox-style';
     * ```
     * Applies properties of the Mapbox Style's first `background` layer to the
     * provided map or VectorTile layer.
     * @param {PluggableMap|VectorTileLayer} mapOrLayer OpenLayers Map or VectorTile layer.
     * @param {Object} glStyle Mapbox Style object.
     */
    export function applyBackground(mapOrLayer, glStyle) {
      glStyle.layers.some(function (l) {
        Eif (l.type == 'background') {
          setBackground(mapOrLayer, l);
          return true;
        }
      });
    }

    function getSourceIdByRef(layers, ref) {
      let sourceId;
      layers.some(function (layer) {
        if (layer.id == ref) {
          sourceId = layer.source;
          return true;
        }
      });
      return sourceId;
    }

    function extentFromTileJSON(tileJSON) {
      const bounds = tileJSON.bounds;
      if (bounds) {
        const ll = fromLonLat([bounds[0], bounds[1]]);
        const tr = fromLonLat([bounds[2], bounds[3]]);
        return [ll[0], ll[1], tr[0], tr[1]];
      }
    }

    /**
     * Creates an OpenLayers VectorTile source for a gl source entry.
     * @param {Object} glSource "source" entry from a Mapbox Style object.
     * @param {string|undefined} url URL to use for the source. This is expected to be the complete http(s) url,
     * with access key applied.When not provided, `glSource.tiles` has to be set.
     * @return {Promise<import("ol/source/VectorTile").default>} Promise resolving to a VectorTile source.
     * @private
     */
    export function setupVectorSource(glSource, url) {
      glSource = assign({}, glSource);
      const cacheKey = [url, JSON.stringify(glSource)].toString();
      let tilejson = tilejsonCache[cacheKey];
      if (!tilejson) {
        tilejson = new TileJSON({
          url: glSource.tiles ? undefined : url,
          tileJSON: glSource.tiles ? glSource : undefined,
        });
        tilejsonCache[cacheKey] = tilejson;
      }
      return new Promise((resolve) => {
        const key = tilejson.on('change', function () {
          const state = tilejson.getState();
          if (state === 'ready') {
            const tileJSONDoc = tilejson.getTileJSON();
            const tiles = Array.isArray(tileJSONDoc.tiles)
              ? tileJSONDoc.tiles
              : [tileJSONDoc.tiles];
            if (url) {
              for (let i = 0, ii = tiles.length; i < ii; ++i) {
                tiles[i] = decodeURI(new URL(tiles[i], url).href);
              }
            }
            const tileGrid = tilejson.getTileGrid();
            const extent = extentFromTileJSON(tileJSONDoc);
            const minZoom = tileJSONDoc.minzoom || 0;
            const maxZoom = tileJSONDoc.maxzoom || 22;
            let source = tilejson.get('ol-source');
            if (source === undefined) {
              source = new VectorTileSource({
                attributions: tilejson.getAttributions(),
                format: new MVT(),
                tileGrid: new TileGrid({
                  origin: tileGrid.getOrigin(0),
                  extent: extent || tileGrid.getExtent(),
                  minZoom: minZoom,
                  resolutions: defaultResolutions.slice(0, maxZoom + 1),
                  tileSize: 512,
                }),
                urls: tiles,
              });
              tilejson.set('ol-source', source);
            }
            unByKey(key);
            resolve(source);
          } else if (state === 'error') {
            tilejson.set('ol-source', null);
            unByKey(key);
            resolve(undefined);
          }
        });
        if (tilejson.getState() === 'ready') {
          tilejson.changed();
        }
      });
    }

    function setupVectorLayer(glSource, url) {
      const layer = new VectorTileLayer({
        declutter: true,
        visible: false,
      });
      setupVectorSource(glSource, url).then((source) => {
        layer.setSource(source);
      });
      return layer;
    }

    function setupRasterLayer(glSource, url) {
      const layer = new TileLayer();
      const source = new TileJSON({
        transition: 0,
        url: glSource.tiles ? undefined : url,
        tileJSON: glSource.tiles ? glSource : undefined,
        crossOrigin: 'anonymous',
      });
      const key = source.on('change', function () {
        const state = source.getState();
        if (state === 'ready') {
          unByKey(key);
          const tileJSONDoc = /** @type {Object} */ (source.getTileJSON());
          const extent = extentFromTileJSON(tileJSONDoc);
          const tileGrid = source.getTileGrid();
          const tileSize = glSource.tileSize || tileJSONDoc.tileSize || 512;
          const minZoom = tileJSONDoc.minzoom || 0;
          const maxZoom = tileJSONDoc.maxzoom || 22;
          // Only works when using ES modules
          //@ts-ignore
          source.tileGrid = new TileGrid({
            origin: tileGrid.getOrigin(0),
            extent: extent || tileGrid.getExtent(),
            minZoom: minZoom,
            resolutions: createXYZ({
              maxZoom: maxZoom,
              tileSize: tileSize,
            }).getResolutions(),
            tileSize: tileSize,
          });
          layer.setSource(source);
        } else Iif (state === 'error') {
          unByKey(key);
          layer.setSource(undefined);
        }
      });
      source.setTileLoadFunction(function (tile, src) {
        if (src.indexOf('{bbox-epsg-3857}') != -1) {
          const bbox = source.getTileGrid().getTileCoordExtent(tile.getTileCoord());
          src = src.replace('{bbox-epsg-3857}', bbox.toString());
        }
        const img = /** @type {import("ol/ImageTile").default} */ (tile).getImage();
        /** @type {HTMLImageElement} */ (img).src = src;
      });
      return layer;
    }

    const geoJsonFormat = new GeoJSON();
    function setupGeoJSONLayer(glSource, path) {
      const data = glSource.data;
      let features, geoJsonUrl;
      if (typeof data == 'string') {
        geoJsonUrl = withPath(data, path);
      } else {
        features = geoJsonFormat.readFeatures(data, {
          featureProjection: 'EPSG:3857',
        });
      }
      return new VectorLayer({
        source: new VectorSource({
          attributions: glSource.attribution,
          features: features,
          format: geoJsonFormat,
          url: geoJsonUrl,
        }),
        visible: false,
      });
    }

    function updateRasterLayerProperties(glLayer, layer, view, functionCache) {
      const zoom = view.getZoom();
      const opacity = getValue(
        glLayer,
        'paint',
        'raster-opacity',
        zoom,
        emptyObj,
        functionCache
      );
      layer.setOpacity(opacity);
    }

    function processStyle(glStyle, map, baseUrl, host, path, accessToken = '') {
      const promises = [];
      let view = map.getView();
      if (
        typeof view.options_ === 'object'
          ? isEmpty(view.options_)
          : !view.isDef() && !view.getRotation() && !view.getResolutions()
      ) {
        view = new View({
          maxResolution: defaultResolutions[0],
        });
        map.setView(view);
      }

      if ('center' in glStyle && !view.getCenter()) {
        view.setCenter(fromLonLat(glStyle.center));
      }
      if ('zoom' in glStyle && view.getZoom() === undefined) {
        view.setResolution(defaultResolutions[0] / Math.pow(2, glStyle.zoom));
      }
      if (!view.getCenter() || view.getZoom() === undefined) {
        view.fit(view.getProjection().getExtent(), {
          nearest: true,
          size: map.getSize(),
        });
      }
      if (glStyle.sprite) {
        Iif (glStyle.sprite.indexOf('mapbox://') == 0) {
          glStyle.sprite = baseUrl + '/sprite' + accessToken;
        } else Eif (glStyle.sprite.indexOf('http') != 0) {
          glStyle.sprite = (host ? host + path : '') + glStyle.sprite + accessToken;
        }
      }

      const glLayers = glStyle.layers;
      let layerIds = [];

      let glLayer, glSource, glSourceId, id, layer, url;
      for (let i = 0, ii = glLayers.length; i < ii; ++i) {
        glLayer = glLayers[i];
        const type = glLayer.type;
        Iif (type == 'heatmap' || type == 'hillshade') {
          //FIXME Unsupported layer type
        } else if (type == 'background') {
          setBackground(map, glLayer);
        } else {
          id = glLayer.source || getSourceIdByRef(glLayers, glLayer.ref);
          // this technique assumes gl layers will be in a particular order
          if (id != glSourceId) {
            if (layerIds.length) {
              promises.push(finalizeLayer(layer, layerIds, glStyle, path, map));
              layerIds = [];
            }
            glSource = glStyle.sources[id];
            url = glSource.url;
            if (url) {
              url = withPath(url, path);
              if (url.indexOf('mapbox://') == 0) {
                const mapid = url.replace('mapbox://', '');
                glSource.tiles = ['a', 'b', 'c', 'd'].map(function (host) {
                  return (
                    'https://' +
                    host +
                    '.tiles.mapbox.com/v4/' +
                    mapid +
                    '/{z}/{x}/{y}.' +
                    (glSource.type == 'vector' ? 'vector.pbf' : 'png') +
                    accessToken
                  );
                });
              } else if (url.indexOf('/') === 0 && host.indexOf('http') === 0) {
                url = host + url;
              }
            }
            if (glSource.tiles) {
              glSource.tiles = glSource.tiles.map((url) => withPath(url, path));
            }

            if (glSource.type == 'vector') {
              layer = setupVectorLayer(glSource, url);
            } else if (glSource.type == 'raster') {
              layer = setupRasterLayer(glSource, url);
              layer.setVisible(
                glLayer.layout ? glLayer.layout.visibility !== 'none' : true
              );
              const functionCache = {};
              view.on(
                'change:resolution',
                updateRasterLayerProperties.bind(
                  this,
                  glLayer,
                  layer,
                  view,
                  functionCache
                )
              );
              updateRasterLayerProperties(glLayer, layer, view, functionCache);
            } else Eif (glSource.type == 'geojson') {
              layer = setupGeoJSONLayer(glSource, path);
            }
            glSourceId = id;
            Eif (layer) {
              layer.set('mapbox-source', glSourceId);
            }
          }
          layerIds.push(glLayer.id);
        }
      }
      promises.push(finalizeLayer(layer, layerIds, glStyle, path, map));
      map.set('mapbox-style', glStyle);
      return Promise.all(promises);
    }

    /**
     * ```js
     * import olms from 'ol-mapbox-style';
     * ```
     *
     * Loads and applies a Mapbox Style object to an OpenLayers Map. This includes
     * the map background, the layers, the center and the zoom.
     *
     * The center and zoom will only be set if present in the Mapbox Style document,
     * and if not already set on the OpenLayers map.
     *
     * Layers will be added to the OpenLayers map, without affecting any layers that
     * might already be set on the map.
     *
     * Layers added by `apply()` will have two additional properties:
     *
     *  * `mapbox-source`: The `id` of the Mapbox Style document's source that the
     *    OpenLayers layer was created from. Usually `apply()` creates one
     *    OpenLayers layer per Mapbox Style source, unless the layer stack has
     *    layers from different sources in between.
     *  * `mapbox-layers`: The `id`s of the Mapbox Style document's layers that are
     *    included in the OpenLayers layer.
     *
     * This function sets an additional `mapbox-style` property on the OpenLayers
     * map instance, which holds the Mapbox Style object.
     *
     * @param {PluggableMap|HTMLElement|string} map Either an existing OpenLayers Map
     * instance, or a HTML element, or the id of a HTML element that will be the
     * target of a new OpenLayers Map.
     * @param {string|Object} style JSON style object or style url pointing to a
     * Mapbox Style object. When using Mapbox APIs, the url must contain an access
     * token and look like
     * `https://api.mapbox.com/styles/v1/mapbox/bright-v9?access_token=[your_access_token_here]`.
     * When passed as JSON style object, all OpenLayers layers created by `apply()`
     * will be immediately available, but they may not have a source yet (i.e. when
     * they are defined by a TileJSON url in the Mapbox Style document). When passed
     * as style url, layers will be added to the map when the Mapbox Style document
     * is loaded and parsed.
     * @return {Promise} A promise that resolves after all layers have been added to
     * the OpenLayers Map instance, their sources set, and their styles applied. the
     * `resolve` callback will be called with the OpenLayers Map instance as
     * argument.
     */
    export default function olms(map, style) {
      let promise,
        accessToken = '',
        baseUrl = '',
        host = '',
        path = '';

      if (typeof map === 'string' || map instanceof HTMLElement) {
        map = new Map({
          target: map,
        });
      }

      if (typeof style === 'string') {
        const parts = style.match(spriteRegEx);
        Iif (parts) {
          baseUrl = parts[1];
          accessToken = parts.length > 2 ? parts[2] : '';
        }
        promise = new Promise(function (resolve, reject) {
          fetch(style, {
            credentials: 'same-origin',
          })
            .then(function (response) {
              return response.json();
            })
            .then(function (glStyle) {
              const a = /** @type {HTMLAnchorElement} */ (
                document.createElement('A')
              );
              a.href = style;
              const href = a.href;
              path = a.pathname.split('/').slice(0, -1).join('/') + '/';
              host = href.substr(0, href.indexOf(path));

              processStyle(glStyle, map, baseUrl, host, path, accessToken)
                .then(function () {
                  resolve(map);
                })
                .catch(reject);
            })
            .catch(function (err) {
              reject(new Error(`Could not load ${style}: ${err.message}`));
            });
        });
      } else {
        promise = new Promise(function (resolve, reject) {
          processStyle(style, map)
            .then(function () {
              resolve(map);
            })
            .catch(reject);
        });
      }

      return promise;
    }

    /**
     * ```js
     * import {apply} from 'ol-mapbox-style';
     * ```
     * Like `olms`, but returns an `ol/Map` instance instead of a `Promise`.
     *
     * @param {PluggableMap|HTMLElement|string} map Either an existing OpenLayers Map
     * instance, or a HTML element, or the id of a HTML element that will be the
     * target of a new OpenLayers Map.
     * @param {string|Object} style JSON style object or style url pointing to a
     * Mapbox Style object. When using Mapbox APIs, the url must contain an access
     * token and look like
     * `https://api.mapbox.com/styles/v1/mapbox/bright-v9?access_token=[your_access_token_here]`.
     * When passed as JSON style object, all OpenLayers layers created by `apply()`
     * will be immediately available, but they may not have a source yet (i.e. when
     * they are defined by a TileJSON url in the Mapbox Style document). When passed
     * as style url, layers will be added to the map when the Mapbox Style document
     * is loaded and parsed.
     * @return {PluggableMap} The OpenLayers Map instance that will be populated with the
     * contents described in the Mapbox Style object.
     */
    export function apply(map, style) {
      if (typeof map === 'string' || map instanceof HTMLElement) {
        map = new Map({
          target: map,
        });
      }
      setTimeout(function () {
        olms(map, style);
      }, 0);
      return map;
    }

    /**
     * If layerIds is not empty, applies the style specified in glStyle to the layer,
     * and adds the layer to the map.
     *
     * The layer may not yet have a source when the function is called.  If so, the style
     * is applied to the layer via a once listener on the 'change:source' event.
     *
     * @param {Layer} layer An OpenLayers layer instance.
     * @param {Array<string>} layerIds Array containing layer ids of already-processed layers.
     * @param {Object} glStyle Style as a JSON object.
     * @param {string|undefined} path The path part of the style URL. Only required
     * when a relative path is used with the `"sprite"` property of the style.
     * @param {PluggableMap} map OpenLayers Map.
     * @return {Promise} Returns a promise that resolves after the source has
     * been set on the specified layer, and the style has been applied.
     * @private
     */
    function finalizeLayer(layer, layerIds, glStyle, path, map) {
      let minZoom = 24;
      let maxZoom = 0;
      const glLayers = glStyle.layers;
      for (let i = 0, ii = glLayers.length; i < ii; ++i) {
        const glLayer = glLayers[i];
        if (layerIds.indexOf(glLayer.id) !== -1) {
          minZoom = Math.min('minzoom' in glLayer ? glLayer.minzoom : 0, minZoom);
          maxZoom = Math.max('maxzoom' in glLayer ? glLayer.maxzoom : 24, maxZoom);
        }
      }
      return new Promise(function (resolve, reject) {
        const setStyle = function () {
          const source = layer.getSource();
          if (!source || source.getState() === 'error') {
            reject(
              new Error(
                'Error accessing data for source ' + layer.get('mapbox-source')
              )
            );
            return;
          }
          if (typeof source.getTileGrid === 'function') {
            const tileGrid = source.getTileGrid();
            if (tileGrid) {
              const sourceMinZoom = tileGrid.getMinZoom();
              if (minZoom > 0 || sourceMinZoom > 0) {
                layer.setMaxResolution(
                  Math.min(
                    defaultResolutions[minZoom],
                    tileGrid.getResolution(sourceMinZoom)
                  ) + 1e-9
                );
              }
              if (maxZoom < 24) {
                layer.setMinResolution(defaultResolutions[maxZoom] + 1e-9);
              }
            }
          }
          if (
            source instanceof VectorSource ||
            source instanceof VectorTileSource
          ) {
            applyStyle(
              /** @type {import("ol/layer/Vector").default|import("ol/layer/VectorTile").default} */ (
                layer
              ),
              glStyle,
              layerIds,
              path
            ).then(
              function () {
                layer.setVisible(true);
                resolve();
              },
              function (e) {
                reject(e);
              }
            );
          } else {
            resolve();
          }
        };

        layer.set('mapbox-layers', layerIds);
        if (map.getLayers().getArray().indexOf(layer) === -1) {
          map.addLayer(layer);
        }

        if (layer.getSource()) {
          setStyle();
        } else {
          layer.once('change:source', setStyle);
        }
      });
    }

    /**
     * ```js
     * import {getLayer} from 'ol-mapbox-style';
     * ```
     * Get the OpenLayers layer instance that contains the provided Mapbox Style
     * `layer`. Note that multiple Mapbox Style layers are combined in a single
     * OpenLayers layer instance when they use the same Mapbox Style `source`.
     * @param {PluggableMap} map OpenLayers Map.
     * @param {string} layerId Mapbox Style layer id.
     * @return {Layer} OpenLayers layer instance.
     */
    export function getLayer(map, layerId) {
      const layers = map.getLayers().getArray();
      for (let i = 0, ii = layers.length; i < ii; ++i) {
        const mapboxLayers = layers[i].get('mapbox-layers');
        Eif (mapboxLayers && mapboxLayers.indexOf(layerId) !== -1) {
          return /** @type {Layer} */ (layers[i]);
        }
      }
    }

    /**
     * ```js
     * import {getLayers} from 'ol-mapbox-style';
     * ```
     * Get the OpenLayers layer instances for the provided Mapbox Style `source`.
     * @param {PluggableMap} map OpenLayers Map.
     * @param {string} sourceId Mapbox Style source id.
     * @return {Array<Layer>} OpenLayers layer instances.
     */
    export function getLayers(map, sourceId) {
      const result = [];
      const layers = map.getLayers().getArray();
      for (let i = 0, ii = layers.length; i < ii; ++i) {
        if (layers[i].get('mapbox-source') === sourceId) {
          result.push(/** @type {Layer} */ (layers[i]));
        }
      }
      return result;
    }

    /**
     * ```js
     * import {getSource} from 'ol-mapbox-style';
     * ```
     * Get the OpenLayers source instance for the provided Mapbox Style `source`.
     * @param {PluggableMap} map OpenLayers Map.
     * @param {string} sourceId Mapbox Style source id.
     * @return {Source} OpenLayers source instance.
     */
    export function getSource(map, sourceId) {
      const layers = map.getLayers().getArray();
      for (let i = 0, ii = layers.length; i < ii; ++i) {
        const source = /** @type {Layer} */ (layers[i]).getSource();
        if (layers[i].get('mapbox-source') === sourceId) {
          return source;
        }
      }
    }

    export {finalizeLayer as _finalizeLayer, getFonts as _getFonts};