/*! UIkit 3.11.2-dev.7dd548556 | https://www.getuikit.com | (c) 2014 - 2022 YOOtheme | MIT License */

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('uikit-util')) :
    typeof define === 'function' && define.amd ? define('uikitslider_parallax', ['uikit-util'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.UIkitSlider_parallax = factory(global.UIkit.util));
})(this, (function (uikitUtil) { 'use strict';

    var Media = {
      props: {
        media: Boolean },


      data: {
        media: false },


      computed: {
        matchMedia() {
          const media = toMedia(this.media);
          return !media || window.matchMedia(media).matches;
        } } };



    function toMedia(value) {
      if (uikitUtil.isString(value)) {
        if (value[0] === '@') {
          const name = "breakpoint-" + value.substr(1);
          value = uikitUtil.toFloat(uikitUtil.getCssVar(name));
        } else if (isNaN(value)) {
          return value;
        }
      }

      return value && !isNaN(value) ? "(min-width: " + value + "px)" : false;
    }

    uikitUtil.memoize(async (src) => {
      if (src) {
        if (uikitUtil.startsWith(src, 'data:')) {
          return decodeURIComponent(src.split(',')[1]);
        } else {
          return (await fetch(src)).text();
        }
      } else {
        return Promise.reject();
      }
    });

    function getMaxPathLength(el) {
      return Math.ceil(
      Math.max(
      0,
      ...uikitUtil.$$('[stroke]', el).map((stroke) => {
        try {
          return stroke.getTotalLength();
        } catch (e) {
          return 0;
        }
      })));


    }

    const props = {
      x: transformFn,
      y: transformFn,
      rotate: transformFn,
      scale: transformFn,
      color: colorFn,
      backgroundColor: colorFn,
      borderColor: colorFn,
      blur: filterFn,
      hue: filterFn,
      fopacity: filterFn,
      grayscale: filterFn,
      invert: filterFn,
      saturate: filterFn,
      sepia: filterFn,
      opacity: cssPropFn,
      stroke: strokeFn,
      bgx: backgroundFn,
      bgy: backgroundFn };


    const { keys } = Object;

    var Parallax = {
      mixins: [Media],

      props: fillObject(keys(props), 'list'),

      data: fillObject(keys(props), undefined),

      computed: {
        props(properties, $el) {
          return keys(props).reduce((result, prop) => {
            if (!uikitUtil.isUndefined(properties[prop])) {
              result[prop] = props[prop](prop, $el, properties[prop].slice());
            }
            return result;
          }, {});
        } },


      events: {
        bgimageload() {
          this.$emit();
        } },


      methods: {
        reset() {
          uikitUtil.each(this.getCss(0), (_, prop) => uikitUtil.css(this.$el, prop, ''));
        },

        getCss(percent) {
          return keys(this.props).reduce(
          (css, prop) => {
            this.props[prop](css, percent);
            return css;
          },
          { transform: '', filter: '' });

        } } };



    function transformFn(prop, el, steps) {
      const unit = getUnit(steps) || { x: 'px', y: 'px', rotate: 'deg' }[prop] || '';

      if (prop === 'x' || prop === 'y') {
        prop = "translate" + uikitUtil.ucfirst(prop);
      }

      steps = steps.map(uikitUtil.toFloat);

      if (steps.length === 1) {
        steps.unshift(prop === 'scale' ? 1 : 0);
      }

      return (css, percent) => {
        let value = getValue(steps, percent);

        if (uikitUtil.startsWith(prop, 'translate')) {
          value = uikitUtil.toFloat(value).toFixed(unit === 'px' ? 0 : 6);
        }

        css.transform += " " + prop + "(" + value + unit + ")";
      };
    }

    function colorFn(prop, el, steps) {
      if (steps.length === 1) {
        steps.unshift(getCssValue(el, prop, ''));
      }

      steps = steps.map((step) => parseColor(el, step));

      return (css, percent) => {
        const [start, end, p] = getStep(steps, percent);
        const value = start.
        map((value, i) => {
          value += p * (end[i] - value);
          return i === 3 ? uikitUtil.toFloat(value) : parseInt(value, 10);
        }).
        join(',');
        css[prop] = "rgba(" + value + ")";
      };
    }

    function parseColor(el, color) {
      return getCssValue(el, 'color', color).
      split(/[(),]/g).
      slice(1, -1).
      concat(1).
      slice(0, 4).
      map(uikitUtil.toFloat);
    }

    function filterFn(prop, el, steps) {
      if (steps.length === 1) {
        steps.unshift(0);
      }

      const unit = getUnit(steps) || { blur: 'px', hue: 'deg' }[prop] || '%';
      prop = { fopacity: 'opacity', hue: 'hue-rotate' }[prop] || prop;
      steps = steps.map(uikitUtil.toFloat);

      return (css, percent) => {
        const value = getValue(steps, percent);
        css.filter += " " + prop + "(" + (value + unit) + ")";
      };
    }

    function cssPropFn(prop, el, steps) {
      if (steps.length === 1) {
        steps.unshift(getCssValue(el, prop, ''));
      }

      steps = steps.map(uikitUtil.toFloat);

      return (css, percent) => {
        css[prop] = getValue(steps, percent);
      };
    }

    function strokeFn(prop, el, steps) {
      if (steps.length === 1) {
        steps.unshift(0);
      }

      const unit = getUnit(steps);
      steps = steps.map(uikitUtil.toFloat);

      if (!steps.some((step) => step)) {
        return uikitUtil.noop;
      }

      const length = getMaxPathLength(el);
      uikitUtil.css(el, 'strokeDasharray', length);

      if (unit === '%') {
        steps = steps.map((step) => step * length / 100);
      }

      steps = steps.reverse();

      return (css, percent) => {
        css.strokeDashoffset = getValue(steps, percent);
      };
    }

    function backgroundFn(prop, el, steps) {
      if (steps.length === 1) {
        steps.unshift(0);
      }

      prop = prop.substr(-1);
      const attr = prop === 'y' ? 'height' : 'width';
      steps = steps.map((step) => uikitUtil.toPx(step, attr, el));

      const bgPos = getCssValue(el, "background-position-" + prop, '');

      return getCssValue(el, 'backgroundSize', '') === 'cover' ?
      backgroundCoverFn(prop, el, steps, bgPos, attr) :
      setBackgroundPosFn(prop, steps, bgPos);
    }

    function backgroundCoverFn(prop, el, steps, bgPos, attr) {
      const dimImage = getBackgroundImageDimensions(el);

      if (!dimImage.width) {
        return uikitUtil.noop;
      }

      const min = Math.min(...steps);
      const max = Math.max(...steps);
      const down = steps.indexOf(min) < steps.indexOf(max);

      const diff = max - min;
      let pos = (down ? -diff : 0) - (down ? min : max);

      const dimEl = {
        width: el.offsetWidth,
        height: el.offsetHeight };


      const baseDim = uikitUtil.Dimensions.cover(dimImage, dimEl);
      const span = baseDim[attr] - dimEl[attr];

      if (span < diff) {
        dimEl[attr] = baseDim[attr] + diff - span;
      } else if (span > diff) {
        const posPercentage = dimEl[attr] / uikitUtil.toPx(bgPos, attr, el, true);

        if (posPercentage) {
          pos -= (span - diff) / posPercentage;
        }
      }

      const dim = uikitUtil.Dimensions.cover(dimImage, dimEl);

      const fn = setBackgroundPosFn(prop, steps, pos + "px");
      return (css, percent) => {
        fn(css, percent);
        css.backgroundSize = dim.width + "px " + dim.height + "px";
        css.backgroundRepeat = 'no-repeat';
      };
    }

    function setBackgroundPosFn(prop, steps, pos) {
      return function (css, percent) {
        css["background-position-" + prop] = "calc(" + pos + " + " + getValue(steps, percent) + "px)";
      };
    }

    const dimensions = {};
    function getBackgroundImageDimensions(el) {
      const src = uikitUtil.css(el, 'backgroundImage').replace(/^none|url\(["']?(.+?)["']?\)$/, '$1');

      if (dimensions[src]) {
        return dimensions[src];
      }

      const image = new Image();
      if (src) {
        image.src = src;

        if (!image.naturalWidth) {
          image.onload = () => {
            dimensions[src] = toDimensions(image);
            uikitUtil.trigger(el, 'bgimageload');
          };
        }
      }

      return dimensions[src] = toDimensions(image);
    }

    function toDimensions(image) {
      return {
        width: image.naturalWidth,
        height: image.naturalHeight };

    }

    function getStep(steps, percent) {
      const count = steps.length - 1;
      const index = Math.min(Math.floor(count * percent), count - 1);

      return steps.
      slice(index, index + 2).
      concat(percent === 1 ? 1 : percent % (1 / count) * count);
    }

    function getValue(steps, percent) {
      const [start, end, p] = getStep(steps, percent);
      return uikitUtil.isNumber(start) ? start + Math.abs(start - end) * p * (start < end ? 1 : -1) : +end;
    }

    function getUnit(steps, defaultUnit) {
      return (
        steps.reduce(
        (unit, step) => unit || uikitUtil.isString(step) && step.replace(/[\d-]/g, '').trim(),
        '') ||
        defaultUnit);

    }

    function getCssValue(el, prop, value) {
      const prev = el.style[prop];
      const val = uikitUtil.css(uikitUtil.css(el, prop, value), prop);
      el.style[prop] = prev;
      return val;
    }

    function fillObject(keys, value) {
      return keys.reduce((data, prop) => {
        data[prop] = value;
        return data;
      }, {});
    }

    var Component = {
      mixins: [Parallax],

      data: {
        selItem: '!li' },


      computed: {
        item(_ref, $el) {let { selItem } = _ref;
          return uikitUtil.query(selItem, $el);
        } },


      events: [
      {
        name: 'itemin itemout',

        self: true,

        el() {
          return this.item;
        },

        handler(_ref2) {let { type, detail: { percent, duration, timing, dir } } = _ref2;
          uikitUtil.fastdom.read(() => {
            const propsFrom = this.getCss(getCurrentPercent(type, dir, percent));
            const propsTo = this.getCss(isIn(type) ? 0.5 : dir > 0 ? 1 : 0);
            uikitUtil.fastdom.write(() => {
              uikitUtil.css(this.$el, propsFrom);
              uikitUtil.Transition.start(this.$el, propsTo, duration, timing).catch(uikitUtil.noop);
            });
          });
        } },


      {
        name: 'transitioncanceled transitionend',

        self: true,

        el() {
          return this.item;
        },

        handler() {
          uikitUtil.Transition.cancel(this.$el);
        } },


      {
        name: 'itemtranslatein itemtranslateout',

        self: true,

        el() {
          return this.item;
        },

        handler(_ref3) {let { type, detail: { percent, dir } } = _ref3;
          uikitUtil.fastdom.read(() => {
            const props = this.getCss(getCurrentPercent(type, dir, percent));
            uikitUtil.fastdom.write(() => uikitUtil.css(this.$el, props));
          });
        } }] };




    function isIn(type) {
      return uikitUtil.endsWith(type, 'in');
    }

    function getCurrentPercent(type, dir, percent) {
      percent /= 2;

      return isIn(type) ^ dir < 0 ? percent : 1 - percent;
    }

    if (typeof window !== 'undefined' && window.UIkit) {
      window.UIkit.component('sliderParallax', Component);
    }

    return Component;

}));
