'use strict';

var vue = require('vue');
var smob = require('smob');

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

var instances = {};
function provide(key, value) {
  instances[key] = value;
}
function inject(key) {
  return instances[key];
}

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */
function setDefaults(input, injectionKey) {
  provide(injectionKey || 'defaults', input);
}
function useDefaults(injectionKey) {
  var defaults = inject(injectionKey || 'defaults');
  if (typeof defaults === 'undefined') {
    return {};
  }
  return defaults;
}

function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly && (symbols = symbols.filter(function (sym) {
      return Object.getOwnPropertyDescriptor(object, sym).enumerable;
    })), keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread2(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
      _defineProperty(target, key, source[key]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
      Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
    });
  }
  return target;
}
function _typeof(obj) {
  "@babel/helpers - typeof";

  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
    return typeof obj;
  } : function (obj) {
    return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  }, _typeof(obj);
}
function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
  return obj;
}

/*
 * Copyright (c) 2021.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

// eslint-disable-next-line @typescript-eslint/ban-types
function hasOwnProperty(obj, prop) {
  return Object.prototype.hasOwnProperty.call(obj, prop);
}

/*
 * Copyright (c) 2022-2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

/**
 * Returns true if either scoped or unscoped named slot exists
 *
 * @returns {Array|undefined} VNodes
 *
 * @param name
 * @param $slots
 */
function hasNormalizedSlot(name) {
  var $slots = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return hasOwnProperty($slots, name);
}

/**
 * Returns VNodes for named slot either scoped or unscoped
 *
 * @param name
 * @param scope
 * @param $slots
 *
 * @returns {Array} VNodes
 */
function normalizeSlot(name) {
  var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var $slots = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  if (hasOwnProperty($slots, name)) {
    return $slots[name](scope);
  }
  return [];
}

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

function isPromise(p) {
  return _typeof(p) === 'object' && p !== null && (p instanceof Promise ||
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  typeof p.then === 'function');
}

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

function initPropertiesFromSource(source, destination) {
  if (_typeof(source) !== 'object') {
    return;
  }
  var keys = Object.keys(destination);
  for (var i = 0; i < keys.length; i++) {
    if (Object.prototype.hasOwnProperty.call(source, keys[i])) {
      var key = keys[i];
      destination[key] = source[keys[i]];
    }
  }
}

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */
function unrefWithDefault(value, alt) {
  var raw = vue.unref(value);
  if (typeof raw === 'undefined') {
    return alt;
  }
  return raw;
}
function setMaybeRefValue(input, value) {
  if (vue.isRef(input)) {
    if (!vue.isReadonly(input)) {
      input.value = value;
    }
  } else {
    input = value;
  }
  return input;
}
function pushMaybeRefArrayValue(input, value) {
  if (vue.isRef(input)) {
    input.value.push(value);
  } else {
    input.push(value);
  }
  return value;
}
function spliceMaybeRefArray(input, index, amount) {
  if (vue.isRef(input)) {
    input.value.splice(index, amount);
  } else {
    input.splice(index, amount);
  }
  return input;
}
function getMaybeRefArrayValue(input, index) {
  if (vue.isRef(input)) {
    return input.value[index];
  }
  return input[index];
}
function extendMaybeRefArrayValue(input, index, value) {
  var item = getMaybeRefArrayValue(input, index);
  var keys = Object.keys(value);
  for (var i = 0; i < keys.length; i++) {
    if (vue.isRef(input)) {
      input.value[index][keys[i]] = item[keys[i]];
    } else {
      input[index][keys[i]] = item[keys[i]];
    }
  }
}
function findIndexOfMaybeRefArray(input, filterFn) {
  if (vue.isRef(input)) {
    return input.value.findIndex(filterFn);
  }
  return input.findIndex(filterFn);
}

/**
 * Transform preset.component.option -> component.option.preset.
 *
 * @param input
 */
function transformPresetsInput(input) {
  var data = {};
  var pKeys = Object.keys(input);
  for (var i = 0; i < pKeys.length; i++) {
    var cKeys = Object.keys(input[pKeys[i]]);
    for (var j = 0; j < cKeys.length; j++) {
      if (!Object.prototype.hasOwnProperty.call(data, cKeys[j])) {
        data[cKeys[j]] = {};
      }
      var oKeys = Object.keys(input[pKeys[i]][cKeys[j]]);
      for (var k = 0; k < oKeys.length; k++) {
        if (!Object.prototype.hasOwnProperty.call(data[cKeys[j]], oKeys[k])) {
          data[cKeys[j]][oKeys[k]] = {};
        }
        data[cKeys[j]][oKeys[k]][pKeys[i]] = input[pKeys[i]][cKeys[j]][oKeys[k]];
      }
    }
  }
  return data;
}
function setPresets(data, injectionKey) {
  var transformedData = transformPresetsInput(data);
  provide(injectionKey || 'presets', transformedData);
}
function setPreset(name, data, injectionKey) {
  var transformedData = transformPresetsInput(_defineProperty({}, name, data));
  var presets = usePresets(injectionKey);
  provide(injectionKey || 'presets', smob.merge(presets, transformedData));
}
function usePresets(injectionKey) {
  var data = inject(injectionKey || 'presets');
  if (typeof data === 'undefined') {
    return {};
  }
  return data;
}

function getGlobalPresetsOptionValue(component, option, injectionKey) {
  var input = usePresets(injectionKey);
  if (!hasOwnProperty(input, component)) {
    return {};
  }
  if (!hasOwnProperty(input[component], option)) {
    return {};
  }
  return input[component][option];
}
function getGlobalComponentOptionValue(component, option, injectionKey) {
  var _useDefaults = useDefaults(injectionKey),
    componentOptions = _useDefaults[component];
  if (_typeof(componentOptions) === 'object' && hasOwnProperty(componentOptions, option)) {
    return componentOptions[option];
  }
  return undefined;
}

function isVNodeClassOption(name) {
  return name.toLowerCase().indexOf('class') !== -1;
}
function isVNodeStyleOption(name) {
  return name.toLowerCase().indexOf('style') !== -1;
}
function isVNodeListenerOption(name) {
  return name.startsWith('on');
}
function isVNodePropsOption(name) {
  return name.toLowerCase().indexOf('props') !== -1;
}
function isVNodeOption(name) {
  return isVNodeClassOption(name) || isVNodeStyleOption(name) || isVNodeListenerOption(name) || isVNodePropsOption(name);
}
function mergeVNodeOption(key, target, source) {
  if (typeof target === 'undefined') {
    return source;
  }
  if (typeof source === 'undefined') {
    return target;
  }
  if (isVNodeClassOption(key)) {
    var _mergeProps = vue.mergeProps({
        "class": target
      }, {
        "class": source
      }),
      clazz = _mergeProps["class"];
    return clazz;
  }
  if (isVNodeStyleOption(key)) {
    var _mergeProps2 = vue.mergeProps({
        style: target
      }, {
        style: source
      }),
      style = _mergeProps2.style;
    return style;
  }
  if (isVNodeListenerOption(key)) {
    var _mergeProps3 = vue.mergeProps(_defineProperty({}, key, target), _defineProperty({}, key, source)),
      listener = _mergeProps3[key];
    return listener;
  }
  if (isVNodePropsOption(key)) {
    return vue.mergeProps(target, source);
  }
  return target !== null && target !== void 0 ? target : source;
}

function isObject(item) {
  return item && _typeof(item) === 'object' && !Array.isArray(item);
}
function mergeOption(key, target, source) {
  if (isVNodeOption(key)) {
    return mergeVNodeOption(key, target, source);
  }
  if (isObject(target) && isObject(source)) {
    return smob.merge(target, source);
  }
  return target !== null && target !== void 0 ? target : source;
}

function isOptionValueConfig(value) {
  if (_typeof(value) !== 'object' || value === null) {
    return false;
  }
  if (!hasOwnProperty(value, 'presets') || !hasOwnProperty(value, 'value')) {
    return false;
  }
  if (_typeof(value.presets) !== 'object' || value.presets === null) {
    return false;
  }
  var keys = Object.keys(value.presets);
  for (var i = 0; i < keys.length; i++) {
    if (!hasOwnProperty(value.presets, keys[i]) || typeof value.presets[keys[i]] !== 'boolean') {
      return false;
    }
  }
  return true;
}
function extractValueFromOptionValueInput(input) {
  if (isOptionValueConfig(input)) {
    return input.value;
  }
  return input;
}

function buildOptionValue(context) {
  var value;
  var presetConfig = {};
  if (isOptionValueConfig(context.value)) {
    value = vue.unref(context.value.value);
    if (context.value.presets) {
      var keys = Object.keys(context.value.presets);
      for (var i = 0; i < keys.length; i++) {
        presetConfig[keys[i]] = context.value.presets[keys[i]];
      }
    }
  } else {
    value = vue.unref(context.value);
  }
  var _ref = context.injectionKeys || {},
    componentsInjectionKey = _ref.components,
    presetsInjectionKey = _ref.presets;
  var configValue = getGlobalComponentOptionValue(context.component, context.key, componentsInjectionKey);
  if (typeof value === 'undefined') {
    value = configValue;
  }
  if (typeof value === 'undefined') {
    value = context.alt;
  }
  if (typeof value === 'undefined') {
    return undefined;
  }
  var presetsValue = getGlobalPresetsOptionValue(context.component, context.key, presetsInjectionKey);
  var presetsKeys = Object.keys(presetsValue);
  for (var i = 0; i < presetsKeys.length; i++) {
    if (!hasOwnProperty(presetConfig, presetsKeys[i]) || presetConfig[presetsKeys[i]]) {
      value = mergeOption(context.key, value, presetsValue[presetsKeys[i]]);
    }
  }
  return value;
}
function buildOptionValueOrFail(context) {
  var target = buildOptionValue(context);
  if (typeof target === 'undefined') {
    throw new Error("A value for option ".concat(context.key, " of component ").concat(context.component, " is required."));
  }
  return target;
}
function createOptionValueBuilderForComponent(component, injectionKeys) {
  return {
    build: function build(context) {
      return buildOptionValue(_objectSpread2(_objectSpread2({}, context), {}, {
        component: component
      }, injectionKeys ? {
        injectionKeys: injectionKeys
      } : {}));
    },
    buildOrFail: function buildOrFail(context) {
      return buildOptionValueOrFail(_objectSpread2(_objectSpread2({}, context), {}, {
        component: component
      }, injectionKeys ? {
        injectionKeys: injectionKeys
      } : {}));
    }
  };
}

exports.buildOptionValue = buildOptionValue;
exports.buildOptionValueOrFail = buildOptionValueOrFail;
exports.createOptionValueBuilderForComponent = createOptionValueBuilderForComponent;
exports.extendMaybeRefArrayValue = extendMaybeRefArrayValue;
exports.extractValueFromOptionValueInput = extractValueFromOptionValueInput;
exports.findIndexOfMaybeRefArray = findIndexOfMaybeRefArray;
exports.getGlobalComponentOptionValue = getGlobalComponentOptionValue;
exports.getGlobalPresetsOptionValue = getGlobalPresetsOptionValue;
exports.getMaybeRefArrayValue = getMaybeRefArrayValue;
exports.hasNormalizedSlot = hasNormalizedSlot;
exports.hasOwnProperty = hasOwnProperty;
exports.initPropertiesFromSource = initPropertiesFromSource;
exports.inject = inject;
exports.isOptionValueConfig = isOptionValueConfig;
exports.isPromise = isPromise;
exports.isVNodeClassOption = isVNodeClassOption;
exports.isVNodeListenerOption = isVNodeListenerOption;
exports.isVNodeOption = isVNodeOption;
exports.isVNodePropsOption = isVNodePropsOption;
exports.isVNodeStyleOption = isVNodeStyleOption;
exports.mergeOption = mergeOption;
exports.mergeVNodeOption = mergeVNodeOption;
exports.normalizeSlot = normalizeSlot;
exports.provide = provide;
exports.pushMaybeRefArrayValue = pushMaybeRefArrayValue;
exports.setDefaults = setDefaults;
exports.setMaybeRefValue = setMaybeRefValue;
exports.setPreset = setPreset;
exports.setPresets = setPresets;
exports.spliceMaybeRefArray = spliceMaybeRefArray;
exports.transformPresetsInput = transformPresetsInput;
exports.unrefWithDefault = unrefWithDefault;
exports.useDefaults = useDefaults;
exports.usePresets = usePresets;
//# sourceMappingURL=index.cjs.js.map
