'use strict';

require('../../../../utils/index.js');
var vue = require('vue');
require('./index.js');
var runtime = require('../../../../utils/props/runtime.js');
var cronExpressionBuilder = require('./cron-expression-builder.js');
var createNamespace = require('../../../../utils/create-namespace.js');

"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 runtime.buildProps({
    value: {
      type: String,
      default: defaultValue
    },
    disabled: {
      type: Boolean,
      default: false
    },
    ...options?.props
  });
}
function useTabEmits() {
  return cronExpressionBuilder.cronExpressionBuilderEmits;
}
function useTabSetup(props, context, options) {
  const { emit } = context;
  const bem = createNamespace.createNamespace("cron-config-list");
  const defaultValue = vue.ref(options?.defaultValue ?? "?");
  const type = vue.ref(options.defaultType ?? "EVERY" /* every */);
  const valueList = vue.ref([]);
  const valueRange = vue.reactive(options.valueRange);
  const valueLoop = vue.reactive(options.valueLoop);
  const valueWeek = vue.reactive(options.valueWeek || {});
  const valueWork = vue.ref(options.valueWork);
  const maxValue = vue.ref(options.maxValue);
  const minValue = vue.ref(options.minValue);
  const computeValue = vue.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 = vue.computed(() => {
    const range = [];
    if (maxValue.value != null) {
      for (let i = minValue.value; i <= maxValue.value; i++) {
        range.push(i);
      }
    }
    return range;
  });
  vue.watch(
    () => props.value,
    (val) => {
      if (val !== computeValue.value) {
        parseValue(val);
      }
    },
    { immediate: true }
  );
  vue.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 = vue.computed(() => ({
    class: ["choice"],
    disabled: props.disabled || vue.unref(options.disabled)
  }));
  const inputNumberAttrs = vue.computed(() => ({
    class: ["w100"],
    max: maxValue.value,
    min: minValue.value,
    precision: 0
  }));
  const typeRangeAttrs = vue.computed(() => ({
    disabled: type.value !== "RANGE" /* range */ || props.disabled || vue.unref(options.disabled),
    ...inputNumberAttrs.value
  }));
  const typeLoopAttrs = vue.computed(() => ({
    disabled: type.value !== "LOOP" /* loop */ || props.disabled || vue.unref(options.disabled),
    ...inputNumberAttrs.value
  }));
  const typeSpecifyAttrs = vue.computed(() => ({
    disabled: type.value !== "SPECIFY" /* specify */ || props.disabled || vue.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
  };
}

exports.TypeEnum = TypeEnum;
exports.useTabEmits = useTabEmits;
exports.useTabProps = useTabProps;
exports.useTabSetup = useTabSetup;
//# sourceMappingURL=use-tab-mixin.js.map
