"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const tslib_es6 = require("../../../../../../../tslib@2.6.2/node_modules/tslib/tslib.es6.js");
const isNumericalString = require("../../utils/is-numerical-string.js");
const isZeroValueString = require("../../utils/is-zero-value-string.js");
const resolveValue = require("../../utils/resolve-value.js");
const index = require("../../value/index.js");
const animatableNone = require("../dom/value-types/animatable-none.js");
const find = require("../dom/value-types/find.js");
const variants = require("./variants.js");
const index$1 = require("../../../../../../../style-value-types@5.0.0/node_modules/style-value-types/dist/es/complex/index.js");
function setMotionValue(visualElement, key, value) {
  if (visualElement.hasValue(key)) {
    visualElement.getValue(key).set(value);
  } else {
    visualElement.addValue(key, index.motionValue(value));
  }
}
function setTarget(visualElement, definition) {
  var resolved = variants.resolveVariant(visualElement, definition);
  var _a = resolved ? visualElement.makeTargetAnimatable(resolved, false) : {}, _b = _a.transitionEnd, transitionEnd = _b === void 0 ? {} : _b;
  _a.transition;
  var target = tslib_es6.__rest(_a, ["transitionEnd", "transition"]);
  target = tslib_es6.__assign(tslib_es6.__assign({}, target), transitionEnd);
  for (var key in target) {
    var value = resolveValue.resolveFinalValueInKeyframes(target[key]);
    setMotionValue(visualElement, key, value);
  }
}
function setVariants(visualElement, variantLabels) {
  var reversedLabels = tslib_es6.__spreadArray([], tslib_es6.__read(variantLabels), false).reverse();
  reversedLabels.forEach(function(key) {
    var _a;
    var variant = visualElement.getVariant(key);
    variant && setTarget(visualElement, variant);
    (_a = visualElement.variantChildren) === null || _a === void 0 ? void 0 : _a.forEach(function(child) {
      setVariants(child, variantLabels);
    });
  });
}
function setValues(visualElement, definition) {
  if (Array.isArray(definition)) {
    return setVariants(visualElement, definition);
  } else if (typeof definition === "string") {
    return setVariants(visualElement, [definition]);
  } else {
    setTarget(visualElement, definition);
  }
}
function checkTargetForNewValues(visualElement, target, origin) {
  var _a, _b, _c;
  var _d;
  var newValueKeys = Object.keys(target).filter(function(key2) {
    return !visualElement.hasValue(key2);
  });
  var numNewValues = newValueKeys.length;
  if (!numNewValues)
    return;
  for (var i = 0; i < numNewValues; i++) {
    var key = newValueKeys[i];
    var targetValue = target[key];
    var value = null;
    if (Array.isArray(targetValue)) {
      value = targetValue[0];
    }
    if (value === null) {
      value = (_b = (_a = origin[key]) !== null && _a !== void 0 ? _a : visualElement.readValue(key)) !== null && _b !== void 0 ? _b : target[key];
    }
    if (value === void 0 || value === null)
      continue;
    if (typeof value === "string" && (isNumericalString.isNumericalString(value) || isZeroValueString.isZeroValueString(value))) {
      value = parseFloat(value);
    } else if (!find.findValueType(value) && index$1.complex.test(targetValue)) {
      value = animatableNone.getAnimatableNone(key, targetValue);
    }
    visualElement.addValue(key, index.motionValue(value));
    (_c = (_d = origin)[key]) !== null && _c !== void 0 ? _c : _d[key] = value;
    visualElement.setBaseTarget(key, value);
  }
}
function getOriginFromTransition(key, transition) {
  if (!transition)
    return;
  var valueTransition = transition[key] || transition["default"] || transition;
  return valueTransition.from;
}
function getOrigin(target, transition, visualElement) {
  var _a, _b;
  var origin = {};
  for (var key in target) {
    origin[key] = (_a = getOriginFromTransition(key, transition)) !== null && _a !== void 0 ? _a : (_b = visualElement.getValue(key)) === null || _b === void 0 ? void 0 : _b.get();
  }
  return origin;
}
exports.checkTargetForNewValues = checkTargetForNewValues;
exports.getOrigin = getOrigin;
exports.getOriginFromTransition = getOriginFromTransition;
exports.setTarget = setTarget;
exports.setValues = setValues;
//# sourceMappingURL=setters.js.map
