import { computed, ref, watch } from "vue";
import { useAlert } from "./alert.js";
import { buildRequiredInputFields } from "./graphqlOperation.js";
import { useGraphqlModelOperation } from "./graphqlModelOperation.js";
export function useGraphqlModelItem(props) {
  const alert = useAlert();
  const modelBy = ref();
  const item = ref();
  const isLoading = ref(false);
  const { operationCreate, operationUpdate, operationDelete, operationRead } = useGraphqlModelOperation(props);
  const fields = computed(() => {
    const fieldsProps = props.fields || [];
    let requiredInputFields = [];
    if (canUpdate.value) {
      requiredInputFields = buildRequiredInputFields(operationUpdate.value?.variables);
    }
    return [.../* @__PURE__ */ new Set([...fieldsProps, ...requiredInputFields])];
  });
  const canCreate = computed(() => {
    return !!operationCreate.value;
  });
  const canUpdate = computed(() => {
    return !!operationUpdate.value;
  });
  const canDelete = computed(() => {
    return !!operationDelete.value;
  });
  function createItem(createItem2, callback) {
    isLoading.value = true;
    return operationCreate.value?.call(fields.value, { input: createItem2 }).then((result) => {
      item.value = result;
    }).catch((error) => {
      alert?.addAlert({ alertType: "error", message: error });
      reload();
    }).finally(() => {
      isLoading.value = false;
      if (callback) callback.done();
    });
  }
  function updateItem(updateItem2, callback) {
    isLoading.value = true;
    return operationUpdate.value?.call(fields.value, { input: updateItem2 }).then((result) => {
      item.value = result;
    }).catch((error) => {
      alert?.addAlert({ alertType: "error", message: error });
      reload();
    }).finally(() => {
      isLoading.value = false;
      if (callback) callback.done();
    });
  }
  function deleteItem(item2, callback) {
    isLoading.value = true;
    return operationDelete.value?.call(fields.value, { input: item2 }).catch((error) => {
      alert?.addAlert({ alertType: "error", message: error });
    }).finally(() => {
      isLoading.value = false;
      reload();
      if (callback) callback.done();
    });
  }
  function reload() {
    isLoading.value = true;
    item.value = void 0;
    operationRead.value?.call(fields.value, computedModelBy.value).then((result) => {
      if (Array.isArray(result)) {
        alert?.addAlert({ alertType: "error", message: "Return result is not single item" });
      } else {
        item.value = result;
      }
    }).catch((error) => {
      alert?.addAlert({ alertType: "error", message: error });
    }).finally(() => {
      isLoading.value = false;
    });
  }
  const computedModelBy = computed(() => {
    return Object.assign({}, props.modelBy, modelBy.value);
  });
  watch([() => props.modelName, () => props.modelKey, computedModelBy], () => {
    reload();
  }, { immediate: true, deep: true });
  return {
    modelBy,
    item,
    operationCreate,
    operationUpdate,
    operationDelete,
    operationRead,
    fields,
    canCreate,
    canUpdate,
    canDelete,
    createItem,
    updateItem,
    deleteItem,
    reload,
    isLoading
  };
}
