import { aJ as isLockedColumn } from '../nitro/nitro.mjs';
import { computed, ref } from 'vue';

const selectedPipeline = ref("");
const pipelineConfig = ref(null);
const isDirty = ref(false);
const loading = ref(false);
const saving = ref(false);
const usePipeline = () => {
  const loadPipeline = async (name) => {
    if (!name) return;
    loading.value = true;
    try {
      const data = await $fetch(`/api/pipelines/${name}`);
      pipelineConfig.value = data;
      isDirty.value = false;
    } finally {
      loading.value = false;
    }
  };
  const savePipeline = async () => {
    if (!pipelineConfig.value || !selectedPipeline.value) return;
    saving.value = true;
    try {
      await $fetch(`/api/pipelines/${selectedPipeline.value}`, {
        method: "PUT",
        body: {
          version: pipelineConfig.value.version,
          description: pipelineConfig.value.description,
          definition: pipelineConfig.value.definition,
          action: pipelineConfig.value.action,
          finish: pipelineConfig.value.finish
        }
      });
      isDirty.value = false;
      return true;
    } finally {
      saving.value = false;
    }
  };
  const markDirty = () => {
    isDirty.value = true;
  };
  const generateSlug = (name) => {
    return name.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
  };
  const allColumns = computed(() => {
    if (!pipelineConfig.value) return [];
    return [
      ...pipelineConfig.value.definition,
      ...pipelineConfig.value.action,
      ...pipelineConfig.value.finish
    ];
  });
  const isValid = computed(() => {
    if (!pipelineConfig.value) return false;
    return pipelineConfig.value.definition.length > 0 && pipelineConfig.value.action.length > 0 && pipelineConfig.value.finish.length > 0;
  });
  const addColumn = (segment, name) => {
    if (!pipelineConfig.value || !name.trim()) return false;
    const slug = generateSlug(name);
    const allSlugs = allColumns.value.map((c) => c.slug.replace(/^\d+_/, ""));
    if (allSlugs.includes(slug)) {
      return false;
    }
    pipelineConfig.value[segment].push({ name, slug });
    markDirty();
    return true;
  };
  const removeColumn = (segment, column) => {
    if (!pipelineConfig.value) return false;
    if (isLockedColumn(column.slug)) {
      return false;
    }
    const index = pipelineConfig.value[segment].findIndex((c) => c.slug === column.slug);
    if (index > -1) {
      pipelineConfig.value[segment].splice(index, 1);
      markDirty();
      return true;
    }
    return false;
  };
  const isColumnInPipeline = (baseSlug) => {
    if (!pipelineConfig.value) return { inPipeline: false };
    const normalizedSlug = baseSlug.replace(/^\d+_/, "");
    for (const segment of ["definition", "action", "finish"]) {
      const found = pipelineConfig.value[segment].find(
        (c) => c.slug.replace(/^\d+_/, "") === normalizedSlug
      );
      if (found) {
        return { inPipeline: true, segment, column: found };
      }
    }
    return { inPipeline: false };
  };
  const addColumnBySlug = (slug, name, segment) => {
    if (!pipelineConfig.value) return false;
    const baseSlug = slug.replace(/^\d+_/, "");
    if (isColumnInPipeline(baseSlug).inPipeline) {
      return false;
    }
    pipelineConfig.value[segment].push({ name, slug: baseSlug });
    markDirty();
    return true;
  };
  const removeColumnBySlug = (baseSlug) => {
    if (!pipelineConfig.value) return false;
    const normalizedSlug = baseSlug.replace(/^\d+_/, "");
    if (isLockedColumn(normalizedSlug)) {
      return false;
    }
    for (const segment of ["definition", "action", "finish"]) {
      const index = pipelineConfig.value[segment].findIndex(
        (c) => c.slug.replace(/^\d+_/, "") === normalizedSlug
      );
      if (index > -1) {
        pipelineConfig.value[segment].splice(index, 1);
        markDirty();
        return true;
      }
    }
    return false;
  };
  const loadActivePipeline = async () => {
    if (pipelineConfig.value) return;
    try {
      const pipelines = await $fetch("/api/pipelines");
      const active = pipelines.find((p) => p.active);
      if (active) {
        selectedPipeline.value = active.name;
        await loadPipeline(active.name);
      }
    } catch {
    }
  };
  return {
    selectedPipeline,
    pipelineConfig,
    isDirty,
    loading,
    saving,
    loadPipeline,
    savePipeline,
    markDirty,
    generateSlug,
    allColumns,
    isValid,
    addColumn,
    removeColumn,
    isColumnInPipeline,
    addColumnBySlug,
    removeColumnBySlug,
    loadActivePipeline
  };
};

export { usePipeline as u };
//# sourceMappingURL=usePipeline-ZPHbtOs6.mjs.map
