import type { SkillConfig } from './skill_config.js';

export interface SkillEnvSchemaItem {
  name: string;
  required: boolean;
  description?: string;
  default?: string;
}

export function buildSkillEnvSchema(config: Pick<SkillConfig, 'env'>): SkillEnvSchemaItem[] | null {
  if (!Array.isArray(config.env) || config.env.length === 0) return null;
  return config.env.map(item => ({
    name: item.name,
    required: !!item.required,
    description: item.description || undefined,
    default: Object.prototype.hasOwnProperty.call(item, 'default') ? item.default : undefined,
  }));
}

export function isSkillEnvSchemaEqual(
  left: SkillEnvSchemaItem[] | null | undefined,
  right: SkillEnvSchemaItem[] | null | undefined,
): boolean {
  const normalizedLeft = left ?? null;
  const normalizedRight = right ?? null;
  if (normalizedLeft === null || normalizedRight === null) {
    return normalizedLeft === normalizedRight;
  }
  if (normalizedLeft.length !== normalizedRight.length) return false;

  return normalizedLeft.every((leftItem, index) => {
    const rightItem = normalizedRight[index];
    return leftItem.name === rightItem.name
      && leftItem.required === rightItem.required
      && leftItem.description === rightItem.description
      && leftItem.default === rightItem.default;
  });
}
