import { Action, type ActionSchema } from './Action.js'
import { observed } from '../../decorators/observed.js'

/**
 * Enables adding a new resource to a collection.
 * Endpoint: POST /[entity-collection-name]
 */
export interface CreateActionSchema extends ActionSchema {
  kind: 'create'
}

/**
 * Enables adding a new resource to a collection.
 * Endpoint: POST /[entity-collection-name]
 */
export class CreateAction extends Action implements CreateActionSchema {
  @observed() override accessor kind: 'create'

  constructor(state: Partial<CreateActionSchema> = {}) {
    super(state)
    this.kind = 'create'
  }

  override toJSON(): CreateActionSchema {
    return {
      ...(super.toJSON() as CreateActionSchema),
      kind: 'create',
    }
  }

  static isCreateAction(action: Action): action is CreateAction {
    return action.kind === 'create'
  }

  static isCreateActionSchema(schema: ActionSchema): schema is CreateActionSchema {
    return schema.kind === 'create'
  }
}
