import 'vue';
export default function useOffset(min, max, step, markList, allowCross, pushable) {
  const formatRangeValue = val => Math.max(min.value, Math.min(max.value, val));
  const formatStepValue = val => {
    if (step.value !== null) {
      const stepValue = min.value + Math.round((formatRangeValue(val) - min.value) / step.value) * step.value;
      // Cut number in case to be like 0.30000000000000004
      const getDecimal = num => (String(num).split('.')[1] || '').length;
      const maxDecimal = Math.max(getDecimal(step.value), getDecimal(max.value), getDecimal(min.value));
      const fixedValue = Number(stepValue.toFixed(maxDecimal));
      return min.value <= fixedValue && fixedValue <= max.value ? fixedValue : null;
    }
    return null;
  };
  const formatValue = val => {
    const formatNextValue = formatRangeValue(val);
    // List align values
    const alignValues = markList.value.map(mark => mark.value);
    if (step.value !== null) {
      alignValues.push(formatStepValue(val));
    }
    // min & max
    alignValues.push(min.value, max.value);
    // Align with marks
    let closeValue = alignValues[0];
    let closeDist = max.value - min.value;
    alignValues.forEach(alignValue => {
      const dist = Math.abs(formatNextValue - alignValue);
      if (dist <= closeDist) {
        closeValue = alignValue;
        closeDist = dist;
      }
    });
    return closeValue;
  };
  // ========================== Offset ==========================
  // Single Value
  const offsetValue = (values, offset, valueIndex, mode = 'unit') => {
    if (typeof offset === 'number') {
      let nextValue;
      const originValue = values[valueIndex];
      // Only used for `dist` mode
      const targetDistValue = originValue + offset;
      // Compare next step value & mark value which is best match
      let potentialValues = [];
      markList.value.forEach(mark => {
        potentialValues.push(mark.value);
      });
      // Min & Max
      potentialValues.push(min.value, max.value);
      // In case origin value is align with mark but not with step
      potentialValues.push(formatStepValue(originValue));
      // Put offset step value also
      const sign = offset > 0 ? 1 : -1;
      if (mode === 'unit') {
        potentialValues.push(formatStepValue(originValue + sign * step.value));
      } else {
        potentialValues.push(formatStepValue(targetDistValue));
      }
      // Find close one
      potentialValues = potentialValues.filter(val => val !== null)
      // Remove reverse value
      .filter(val => offset < 0 ? val <= originValue : val >= originValue);
      if (mode === 'unit') {
        // `unit` mode can not contain itself
        potentialValues = potentialValues.filter(val => val !== originValue);
      }
      const compareValue = mode === 'unit' ? originValue : targetDistValue;
      nextValue = potentialValues[0];
      let valueDist = Math.abs(nextValue - compareValue);
      potentialValues.forEach(potentialValue => {
        const dist = Math.abs(potentialValue - compareValue);
        if (dist < valueDist) {
          nextValue = potentialValue;
          valueDist = dist;
        }
      });
      // Out of range will back to range
      if (nextValue === undefined) {
        return offset < 0 ? min.value : max.value;
      }
      // `dist` mode
      if (mode === 'dist') {
        return nextValue;
      }
      // `unit` mode may need another round
      if (Math.abs(offset) > 1) {
        const cloneValues = [...values];
        cloneValues[valueIndex] = nextValue;
        return offsetValue(cloneValues, offset - sign, valueIndex, mode);
      }
      return nextValue;
    } else if (offset === 'min') {
      return min.value;
    } else if (offset === 'max') {
      return max.value;
    }
  };
  /** Same as `offsetValue` but return `changed` mark to tell value changed */
  const offsetChangedValue = (values, offset, valueIndex, mode = 'unit') => {
    const originValue = values[valueIndex];
    const nextValue = offsetValue(values, offset, valueIndex, mode);
    return {
      value: nextValue,
      changed: nextValue !== originValue
    };
  };
  const needPush = dist => {
    const pushableValue = pushable.value;
    return pushableValue === null && dist === 0 || typeof pushableValue === 'number' && dist < pushableValue;
  };
  // Values
  const offsetValues = (values, offset, valueIndex, mode = 'unit') => {
    const nextValues = values.map(formatValue);
    const originValue = nextValues[valueIndex];
    const nextValue = offsetValue(nextValues, offset, valueIndex, mode);
    nextValues[valueIndex] = nextValue;
    if (allowCross.value === false) {
      // >>>>> Allow Cross
      const pushNum = pushable.value || 0;
      // ============ AllowCross ===============
      if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) {
        nextValues[valueIndex] = Math.max(nextValues[valueIndex], nextValues[valueIndex - 1] + pushNum);
      }
      if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) {
        nextValues[valueIndex] = Math.min(nextValues[valueIndex], nextValues[valueIndex + 1] - pushNum);
      }
    } else if (typeof pushable === 'number' || pushable === null) {
      // >>>>> Pushable
      // =============== Push ==================
      // >>>>>> Basic push
      // End values
      for (let i = valueIndex + 1; i < nextValues.length; i += 1) {
        let changed = true;
        while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
          ({
            value: nextValues[i],
            changed
          } = offsetChangedValue(nextValues, 1, i));
        }
      }
      // Start values
      for (let i = valueIndex; i > 0; i -= 1) {
        let changed = true;
        while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
          ({
            value: nextValues[i - 1],
            changed
          } = offsetChangedValue(nextValues, -1, i - 1));
        }
      }
      // >>>>> Revert back to safe push range
      // End to Start
      for (let i = nextValues.length - 1; i > 0; i -= 1) {
        let changed = true;
        while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
          ({
            value: nextValues[i - 1],
            changed
          } = offsetChangedValue(nextValues, -1, i - 1));
        }
      }
      // Start to End
      for (let i = 0; i < nextValues.length - 1; i += 1) {
        let changed = true;
        while (needPush(nextValues[i + 1] - nextValues[i]) && changed) {
          ({
            value: nextValues[i + 1],
            changed
          } = offsetChangedValue(nextValues, 1, i + 1));
        }
      }
    }
    return {
      value: nextValues[valueIndex],
      values: nextValues
    };
  };
  return [formatValue, offsetValues];
}