import '../../../../utils/index.mjs';
import { ref, reactive, computed, watch, unref } from 'vue';
import './index.mjs';
import { buildProps } from '../../../../utils/props/runtime.mjs';
import { cronExpressionBuilderEmits } from './cron-expression-builder.mjs';
import { createNamespace } from '../../../../utils/create-namespace.mjs';

"use strict";
var TypeEnum = /* @__PURE__ */ ((TypeEnum2) => {
  TypeEnum2["unset"] = "UNSET";
  TypeEnum2["every"] = "EVERY";
  TypeEnum2["range"] = "RANGE";
  TypeEnum2["loop"] = "LOOP";
  TypeEnum2["work"] = "WORK";
  TypeEnum2["last"] = "LAST";
  TypeEnum2["specify"] = "SPECIFY";
  return TypeEnum2;
})(TypeEnum || {});
function useTabProps(options) {
  const defaultValue = options?.defaultValue ?? "?";
  return buildProps({
    value: {
      type: String,
      default: defaultValue
    },
    disabled: {
      type: Boolean,
      default: false
    },
    ...options?.props
  });
}
function useTabEmits() {
  return cronExpressionBuilderEmits;
}
function useTabSetup(props, context, options) {
  const { emit } = context;
  const bem = createNamespace("cron-config-list");
  const defaultValue = ref(options?.defaultValue ?? "?");
  const type = ref(options.defaultType ?? "EVERY" /* every */);
  const valueList = ref([]);
  const valueRange = reactive(options.valueRange);
  const valueLoop = reactive(options.valueLoop);
  const valueWeek = reactive(options.valueWeek || {});
  const valueWork = ref(options.valueWork);
  const maxValue = ref(options.maxValue);
  const minValue = ref(options.minValue);
  const computeValue = computed(() => {
    const valueArray = [];
    switch (type.value) {
      case "UNSET" /* unset */:
        valueArray.push("?");
        break;
      case "EVERY" /* every */:
        valueArray.push("*");
        break;
      case "RANGE" /* range */:
        valueArray.push(`${valueRange.start}-${valueRange.end}`);
        break;
      case "LOOP" /* loop */:
        valueArray.push(`${valueLoop.start}/${valueLoop.interval}`);
        break;
      case "WORK" /* work */:
        valueArray.push(`${valueWork.value}W`);
        break;
      case "LAST" /* last */:
        valueArray.push("L");
        break;
      case "SPECIFY" /* specify */:
        if (valueList.value.length === 0) {
          valueList.value.push(minValue.value);
        }
        valueArray.push(valueList.value.join(","));
        break;
      default:
        valueArray.push(defaultValue.value);
        break;
    }
    return valueArray.length > 0 ? valueArray.join("") : defaultValue.value;
  });
  const specifyRange = computed(() => {
    const range = [];
    if (maxValue.value != null) {
      for (let i = minValue.value; i <= maxValue.value; i++) {
        range.push(i);
      }
    }
    return range;
  });
  watch(
    () => props.value,
    (val) => {
      if (val !== computeValue.value) {
        parseValue(val);
      }
    },
    { immediate: true }
  );
  watch(computeValue, (v) => updateValue(v));
  function updateValue(value) {
    emit("change", value);
    emit("update:value", value);
  }
  function parseValue(value) {
    if (value === computeValue.value) {
      return;
    }
    try {
      if (!value || value === defaultValue.value) {
        type.value = "EVERY" /* every */;
      } else if (value.indexOf("?") >= 0) {
        type.value = "UNSET" /* unset */;
      } else if (value.indexOf("-") >= 0) {
        type.value = "RANGE" /* range */;
        const values = value.split("-");
        if (values.length >= 2) {
          valueRange.start = parseInt(values[0]);
          valueRange.end = parseInt(values[1]);
        }
      } else if (value.indexOf("/") >= 0) {
        type.value = "LOOP" /* loop */;
        const values = value.split("/");
        if (values.length >= 2) {
          valueLoop.start = value[0] === "*" ? 0 : parseInt(values[0]);
          valueLoop.interval = parseInt(values[1]);
        }
      } else if (value.indexOf("W") >= 0) {
        type.value = "WORK" /* work */;
        const values = value.split("W");
        if (!values[0] && !isNaN(values[0])) {
          valueWork.value = parseInt(values[0]);
        }
      } else if (value.indexOf("L") >= 0) {
        type.value = "LAST" /* last */;
      } else if (value.indexOf(",") >= 0 || !isNaN(value)) {
        type.value = "SPECIFY" /* specify */;
        valueList.value = value.split(",").map((item) => parseInt(item));
      } else {
        type.value = "EVERY" /* every */;
      }
    } catch (e) {
      type.value = "EVERY" /* every */;
    }
  }
  const beforeRadioAttrs = computed(() => ({
    class: ["choice"],
    disabled: props.disabled || unref(options.disabled)
  }));
  const inputNumberAttrs = computed(() => ({
    class: ["w100"],
    max: maxValue.value,
    min: minValue.value,
    precision: 0
  }));
  const typeRangeAttrs = computed(() => ({
    disabled: type.value !== "RANGE" /* range */ || props.disabled || unref(options.disabled),
    ...inputNumberAttrs.value
  }));
  const typeLoopAttrs = computed(() => ({
    disabled: type.value !== "LOOP" /* loop */ || props.disabled || unref(options.disabled),
    ...inputNumberAttrs.value
  }));
  const typeSpecifyAttrs = computed(() => ({
    disabled: type.value !== "SPECIFY" /* specify */ || props.disabled || unref(options.disabled),
    class: ["list-check-item"]
  }));
  return {
    type,
    bem,
    TypeEnum,
    defaultValue,
    valueRange,
    valueLoop,
    valueWeek,
    valueList,
    valueWork,
    maxValue,
    minValue,
    computeValue,
    specifyRange,
    updateValue,
    parseValue,
    beforeRadioAttrs,
    inputNumberAttrs,
    typeRangeAttrs,
    typeLoopAttrs,
    typeSpecifyAttrs
  };
}

export { TypeEnum, useTabEmits, useTabProps, useTabSetup };
//# sourceMappingURL=use-tab-mixin.mjs.map
