'use strict';

function fieldsToProperties(fields, components, includeStepGroup = false) {
  const properties = {};
  for (const field of fields) {
    const component = components.find((c) => c.type === field.type);
    if (!component) {
      console.warn(`Unknown component type: ${field.type}`);
      continue;
    }
    const isRequired = field.props.required ?? false;
    const schemaProp = component.toJSONSchema(field.props, isRequired);
    if (includeStepGroup && field.stepGroup !== void 0) {
      schemaProp.stepGroup = field.stepGroup;
    }
    if (field.type === "object" && field.children && field.children.length > 0) {
      schemaProp.properties = fieldsToProperties(field.children, components, false);
      const childRequired = field.children.filter((child) => child.props.required).map((child) => child.id);
      if (childRequired.length > 0) {
        schemaProp.required = childRequired;
      }
    }
    if (field.type === "array" && field.itemTemplate && field.itemTemplate.length > 0) {
      schemaProp.items = {
        type: "object",
        properties: fieldsToProperties(field.itemTemplate, components, false)
      };
      const itemRequired = field.itemTemplate.filter((item) => item.props.required).map((item) => item.id);
      if (itemRequired.length > 0) {
        schemaProp.items.required = itemRequired;
      }
    }
    properties[field.id] = schemaProp;
  }
  return properties;
}
function getRequiredFieldIds(fields) {
  return fields.filter((f) => f.props.required).map((f) => f.id);
}
function fieldsToJSONSchema(fields, components, steps) {
  const hasMultipleSteps = steps && steps.length > 1;
  const properties = fieldsToProperties(fields, components, hasMultipleSteps);
  const required = getRequiredFieldIds(fields);
  return {
    type: "object",
    properties,
    ...required.length > 0 ? { required } : {},
    // Only include steps if there are multiple steps
    ...hasMultipleSteps ? { steps } : {}
  };
}
function propertiesToFields(properties, requiredSet, components) {
  const fields = [];
  for (const [key, prop] of Object.entries(properties)) {
    const isRequired = requiredSet.has(key);
    let field = null;
    for (const component of components) {
      field = component.fromJSONSchema(prop, key, isRequired);
      if (field) {
        break;
      }
    }
    if (field) {
      if (prop.stepGroup !== void 0) {
        field.stepGroup = prop.stepGroup;
      }
      if (field.type === "object" && prop.properties) {
        const childRequiredSet = new Set(prop.required || []);
        field.children = propertiesToFields(prop.properties, childRequiredSet, components);
      }
      if (field.type === "array" && prop.items?.properties) {
        const itemRequiredSet = new Set(prop.items.required || []);
        field.itemTemplate = propertiesToFields(prop.items.properties, itemRequiredSet, components);
      }
      fields.push(field);
    } else {
      console.warn(`Could not parse JSON Schema property: ${key}`, prop);
      fields.push({
        id: key,
        type: "text",
        props: {
          label: prop.title || key,
          description: prop.description,
          placeholder: prop.placeholder,
          required: isRequired
        },
        // Include stepGroup even for fallback fields
        ...prop.stepGroup !== void 0 ? { stepGroup: prop.stepGroup } : {}
      });
    }
  }
  return fields;
}
function jsonSchemaToFieldsAndSteps(schema, components) {
  if (!schema || !schema.properties) {
    return { fields: [], steps: [] };
  }
  const requiredSet = new Set(schema.required || []);
  const fields = propertiesToFields(schema.properties, requiredSet, components);
  const steps = schema.steps || [];
  return { fields, steps };
}
function generateFieldId(prefix = "field") {
  return `${prefix}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
function generateStepId() {
  return `step_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
function createStep(index) {
  return {
    id: generateStepId(),
    title: `Step ${index + 1}`
  };
}

exports.createStep = createStep;
exports.fieldsToJSONSchema = fieldsToJSONSchema;
exports.generateFieldId = generateFieldId;
exports.generateStepId = generateStepId;
exports.jsonSchemaToFieldsAndSteps = jsonSchemaToFieldsAndSteps;
