import { module, test } from "qunit";
import Category from "discourse/models/category";
import NavItem from "discourse/models/nav-item";
import Site from "discourse/models/site";
import { run } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";

module("Unit | Model | nav-item", function (hooks) {
  setupTest(hooks);

  hooks.beforeEach(function () {
    run(function () {
      const store = getOwner(this).lookup("service:store");
      const fooCategory = store.createRecord("category", {
        slug: "foo",
        id: 123,
      });
      Site.currentProp("categories").addObject(fooCategory);
    });
  });

  test("href", function (assert) {
    assert.expect(4);

    function href(text, opts, expected, label) {
      assert.strictEqual(NavItem.fromText(text, opts).href, expected, label);
    }

    href("latest", {}, "/latest", "latest");
    href("categories", {}, "/categories", "categories");
    href("latest", { tagId: "bar" }, "/tag/bar/l/latest", "latest with tag");
    href(
      "latest",
      { tagId: "bar", category: Category.findBySlugPath(["foo"]) },
      "/tags/c/foo/123/bar/l/latest",
      "latest with tag and category"
    );
  });

  test("count", function (assert) {
    const store = getOwner(this).lookup("service:store");
    const navItem = store.createRecord("nav-item", { name: "new" });

    assert.strictEqual(navItem.count, 0, "it has no count by default");

    navItem.topicTrackingState.modifyState("t1", {
      topic_id: 1,
      last_read_post_number: null,
      created_in_new_period: true,
    });
    navItem.topicTrackingState.incrementMessageCount();

    assert.strictEqual(
      navItem.count,
      1,
      "it updates when a new message arrives"
    );
  });

  test("displayName", function (assert) {
    const store = getOwner(this).lookup("service:store");
    const navItem = store.createRecord("nav-item", {
      name: "something",
    });

    assert.strictEqual(
      navItem.displayName,
      "[en.filters.something.title count=0]"
    );

    navItem.set("displayName", "Extra Item");
    assert.strictEqual(navItem.displayName, "Extra Item");
  });

  test("title", function (assert) {
    const store = getOwner(this).lookup("service:store");
    const navItem = store.createRecord("nav-item", {
      name: "something",
    });

    assert.strictEqual(navItem.title, "[en.filters.something.help]");

    navItem.set("title", "Extra Item");
    assert.strictEqual(navItem.title, "Extra Item");
  });
});
